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);
Monday, 11 January 2021
How to get last insert id?
Subscribe to:
Post Comments (Atom)
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...
-
Step1: composer create-project laravel/laravel canpac Step2: cd canpac Step3: composer require laravel/ui Step4: php artisan ui bootst...
-
1. Create a middleware name PostShortCodeMiddleware 2. Add this middleware in the kernal file. 3. Create a shortcode.php and shorcodeurl.php...
No comments:
Post a Comment