Monday, 11 January 2021

How to get last insert id?

 In laravel, when you go with DB query builder then you can use insertGetId() that will insert a record and then return last inserted record id.
Example:
 
        $id = DB::table('users')->insertGetId(
                ['email' => 'rameshyadavjee@gmail.com', 'name' => 'Ramesh']
        );
        print_r($id);
   

If you are inserting a record using Laravel Eloquent then you don't need to use insertGetId() function to get last inserted id. You can simply use create method to insert a new record into. The inserted model instance will be returned to you from the method.
Example:


        $user = User::create(['name'=>'Ramesh','email'=>'rameshyadavjee@gmail.com']);
        print_r($user->id);
 

No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...