Search code examples
laravellaravel-query-builderlast-insert-id

I cant get lastInsertId on laravel 7 project


$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.


Solution

  • 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();