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

lagbox
  • 48,571
  • 8
  • 72
  • 83
Heathcliff
  • 17
  • 5
  • Welcome to SO ... the `insert` method doesn't return an object you could call methods on, it returns a `bool` .., you could get the underlying PDO instance from the connection and call that method on it though: `DB::getPdo()->lastInsertId()` – lagbox Dec 12 '21 at 04:10
  • Thanks, its gets id and the product tables id and images tables ids are same now but looks not linked is it okay ? – Heathcliff Dec 12 '21 at 04:26
  • idk what you mean by linked, you are inserting a single record into one table here – lagbox Dec 12 '21 at 04:27
  • now i edited main post and thanks for helping me ^^ here is the pictures and codes – Heathcliff Dec 12 '21 at 04:37
  • what is the issue with the new part there? – lagbox Dec 12 '21 at 04:39
  • so tried to add multiple images to mysql but its saves first image that i chosen so tried on php its works fine but not laravel – Heathcliff Dec 12 '21 at 04:41
  • uploaded files would have to be moved to a permanent location then you could get that path and store in the database, but that is outside the scope of this question .... is there a problem with the loop inserting records in the database? – lagbox Dec 12 '21 at 04:42
  • i think i cant use SO properly sorry should i post another questions ? – Heathcliff Dec 12 '21 at 04:45
  • it depends what you are trying to ask about now ... in reality your original question is answered and solved, but you seem to have more database issues to work on – lagbox Dec 12 '21 at 04:46
  • 1
    and thanks for helping me ^^ – Heathcliff Dec 12 '21 at 04:46
  • I am going to revert your question to the original version and you can create a new question about any further questions, about image upload or the next set of database queries, etc ... good luck – lagbox Dec 12 '21 at 04:55
  • please feel free to accept an answer if they helped you out – lagbox Dec 12 '21 at 06:08
  • done ^^ i didnt know it accepts answer thanks ^^ – Heathcliff Dec 12 '21 at 06:13

4 Answers4

1

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' => 'john@example.com', 'votes' => 0]
);

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

Tipu Sultan Eiko
  • 677
  • 4
  • 10
0

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();
lagbox
  • 48,571
  • 8
  • 72
  • 83
0
        $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;
Arafat Rahman
  • 993
  • 5
  • 19
  • 46
-1

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.

Ripon Uddin
  • 709
  • 3
  • 14
  • 29