azim
azim

Reputation: 67

how to use create() method in laravel DB class

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']
        ]);

Upvotes: 0

Views: 1251

Answers (3)

Uğur Arıcı
Uğur Arıcı

Reputation: 1280

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']
        ]);

Upvotes: 1

Suraj Singh
Suraj Singh

Reputation: 449

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.

Upvotes: 1

Can Vural
Can Vural

Reputation: 2067

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']
   ]);

Upvotes: 1

Related Questions