0

I want to create a new row using the following SQL instead of eloquent

the code I am trying with :

$create_transections = DB::table('package_plan_fees')
        ->create([
            'paid_amount' => $post_data['total_amount'],
            'enroll_able' => $post_data['enrollable'],
            'user_id' => $post_data['user_id'],
            'package_id' => $post_data['package_id'],
            'plan_id' =>  $post_data['plan_id'],
            'status' =>  $post_data['status']
        ]);

3 Answers 3

1

You need to use insert instead of create.

You can check Insert Statements from Laravel documentation.

Your code needs to be like this:

$create_transections = DB::table('package_plan_fees')
        ->insert([
            'paid_amount' => $post_data['total_amount'],
            'enroll_able' => $post_data['enrollable'],
            'user_id' => $post_data['user_id'],
            'package_id' => $post_data['package_id'],
            'plan_id' =>  $post_data['plan_id'],
            'status' =>  $post_data['status']
        ]);

But because you said you want to get inserted row's id, you can use insertGetId method which described in Auto-Incrementing IDs section of Laravel Documentation.

$inserted_rows_id = DB::table('package_plan_fees')
        ->insertGetId([
            'paid_amount' => $post_data['total_amount'],
            'enroll_able' => $post_data['enrollable'],
            'user_id' => $post_data['user_id'],
            'package_id' => $post_data['package_id'],
            'plan_id' =>  $post_data['plan_id'],
            'status' =>  $post_data['status']
        ]);
Sign up to request clarification or add additional context in comments.

5 Comments

but I need to get new created row's ID by this code 'package_plan_fee_id' => $create_transections->id,
You can achieve it by using insertGetId method. Just replace insert on my answer with insertGetId and it will return inserted row's id value. To make it work the package_plan_fees table must have an auto-incrementing id field.
thanks for your help bro, I am sorry I have to accept the first one
Actually my answer is first one. My answers creation time is: "2021-08-12 12:01:11Z" and other's is "2021-08-12 12:01:40Z" My answer is 29 seconds early. You can check by hover your mouse over answer time. Because I updated my answer after you wanted to learn about id and it shows after that on listing. It is not a problem, happy to solve your problem.
Uğur Arıcı ok bro, I changed the mark, thanks again
1

There is no create method. You need to use insert. Relevant documentation page is here.

DB::table('package_plan_fees')
    ->insert([
        'paid_amount' => $post_data['total_amount'],
        'enroll_able' => $post_data['enrollable'],
        'user_id' => $post_data['user_id'],
        'package_id' => $post_data['package_id'],
        'plan_id' =>  $post_data['plan_id'],
        'status' =>  $post_data['status']
   ]);

2 Comments

but I need to get new created row's ID
@azim Then you can use insertGetId It'll return the id
1
create_transections = DB::table('package_plan_fees')
        ->insertGetId([
            'paid_amount' => $post_data['total_amount'],
            'enroll_able' => $post_data['enrollable'],
            'user_id' => $post_data['user_id'],
            'package_id' => $post_data['package_id'],
            'plan_id' =>  $post_data['plan_id'],
            'status' =>  $post_data['status']
        ]);

In order to insert & get inserted id back, you need to use insertGetId() method.

2 Comments

thanks for your help bro, I am sorry I have to accept the first one
@azim That's fine bro. You got the answer, that matters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.