Home > front end >  I cant get lastInsertId on laravel 7 project
I cant get lastInsertId on laravel 7 project

Time:12-12

$sql = DB::table('laravel_products')
    ->insert(array(
        'name' => $name,
        'price' => $price,
        'qty' => $qty,
        'description' => $description,
        'uruu' => $uruu,
        'garage' => $garage,
        'duureg' => $duureg,
        'tagt' => $tagt,
        'talbai' => $talbai,
        'haalga' => $haalga,
        'tsonh' => $tsonh,
        'shal' => $shal,
        'tsonhtoo' => $ttsonh,
        'hdawhar' => $bdawhar,
        'lizing' => $lizing,
        'utas' => $utas,
        'email' => $email,
        'hereg' => $hereg,
        'bairshil' => $bairshil,
        'bairlal' => $bairlal,
        'ashig' => $ashigon,
        'zahi' => $zahi,
        'image' => $data
    ));

$lastInsertedID = $sql->lastInsertId();

When I try to insert its responses:

"Call to a member function lastInsertId() on bool"

I used insertGetId but its cant save multiple rows of pictures on mysql.

CodePudding user response:

If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:

$id = DB::table('users')->insertGetId(
    ['email' => '[email protected]', 'votes' => 0]
);

from : https://laravel.com/docs/5.8/queries#inserts

CodePudding user response:

        $data = new LaravelProducts(); //LaravelProducts is your Model Name
    
        $data->name= $name; //here 'name' is your column name
        $data->price= $price; //here 'price' is your column name
        $data->qty= $qty; //here 'qty' is your column name
        $data->description= $description; //here 'description' is your column name
        ..........
        ..........
        $data->image= $image; //here 'image' is your column name

        $data->save();
        
        $lastInsertedId = $data->id;

CodePudding user response:

If you want to get the last inserted ID like that you can call that method on the PDO instance directly:

$id = DB::getPdo()->lastInsertId();

CodePudding user response:

You don't have to write a new query to collect last inserted id from database.

$laravel_product = DB::table('laravel_products')
             ->insertGetId( array(
                 'name' => $name,
                 'price' => $price,
                 'qty' => $qty,
                 'description' => $description,
                 'uruu' => $uruu,
                 'garage' => $garage,
                 'duureg' => $duureg,
                 'tagt' => $tagt,
                 'talbai' => $talbai,
                 'haalga' => $haalga,
                 'tsonh' => $tsonh,
                 'shal' => $shal,
                 'tsonhtoo' => $ttsonh,
                 'hdawhar' => $bdawhar,
                 'lizing' => $lizing,
                 'utas' => $utas,
                 'email' => $email,
                 'hereg' => $hereg,
                 'bairshil' => $bairshil,
                 'bairlal' => $bairlal,
                 'ashig' => $ashigon,
                 'zahi' => $zahi,
                 'image' => $data
                 )
             );

  foreach ($filenamesToSave as $filename) {
            DB::insert('INSERT INTO laravel_products_images ( product_id, filename ) VALUES ( ?, ? )',[$laravel_product->id, $filename]);
            return view('createproduct');
        } // Foreach Closing

 // Echo your inserted ID Like Below
 echo $laravel_product->id;

It should be 100% working for you.

  • Related