Here I am using laravel 5.5. My code is
$result = DB::insert('INSERT INTO .......');
Here it returns true. But how can I get inserted id? In my table id is the primary key. Thanks in advance
you can use insertGetId().
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]
);
doc can be found here.
Instead of using DB method you can simply use Laravel eloquent:
$result = <YOUR_MODEL_NAME>::create(<YOUR_DATA>)->id();
it return the last inserted record id.
And make sure if you use this method you need to add $fillable in your MODEL like:
class <YOUR_MODEL> extends Model
{
protected $fillable = [ 'column_name_1', 'column_name_2', .., 'column_name_n' ];
}
Why don't you order your rows and get last id?
select <primary key> from <table> order by desc limit 1;
$result = MODEL_NAME::create(data);
return $result->id;
try this may be it's working
Related
I am performing a database operation with "Eloquent ORM in Laravel". I just want to take the last insert id (not the maximum id) in the database.
I searched to get last insert id in laravel Eloquent ORM, I got following link (Laravel, get last insert id using Eloquent) that is refer to get last insert id from following function "$data->save()".
But I need to get
"Model::insert($data)";
My Query:
$insert = Product::insert($alldata);
if ($insert) {
return response()->json([
'success' => 'Ürün başarıyla Eklendi.',
'id' => ???????
]);
}
How can I retrieve the last inserted id?
$insert = Product::create($alldata);
$insert->id
Holds the id of the inserted item.
Basically you have the whole collection in your $insert variable, of the item that you inserted.
You can Also Use insertGetId() function
$id = DB::table('table_name')-> insertGetId($alldata);
I'm trying to get the same result as in Eloquent when create and update will return the model back.
In the case of query builder, how can I return the model when insert returns boolean and update returns the ID of the updated row?!?!?
For example: DB::table('users')->insert( ['email' => 'john#example.com', 'votes' => 0] );
Will return boolean, instead what I am looking for is to get that created user, same behavior with User::create().
DB::table('users')->update() will return the ID of the updated row, again I want the updated row as an object same behavior with User::update().
I know that insertGetId() will give me the ID when I use insert(), but I don't want to make an extra step to use the ID and find the row.
Also, for the update, I don't want to use the ID that update() returns to use it in order to find the row.
Does that make any sense?
Use insertGetId() instead of insert() and then find() the model. For example:
$id = DB::table('users')->insertGetId(['name' => 'Ivanka', 'email' => 'ivanka#ivanka.com']);
$user = User::find($id);
When you are updating you have the id, so just find() it: User::find($id).
Documentation that explains how insertGetId() works: https://laravel.com/docs/5.7/queries#inserts
also, you can use from below code if you want.
find the last insert column:
$last_id = Illuminate\Support\Facades\DB::getPdo()->lastInsertId();
and next:
$user = User::find($last_id);
i am new in laravel and have to save the record in table , i am using this code to insert the value
$model->studentRollId = $value1;
$model->resident_permit_number = $value2;
$model->save()
i am not getting any error but record not inserting in the table , so can anyone please tell me how can i print the executed query by the $model->save(), i tried DB::getQueryLog() but its not shiwing the query log so i can fin what is the issue . thanks in advance
Use ->toSql(), after your $model->save(), just do a dd($model->toSql())
Edit: Have you do a \DB::enableQueryLog();?
\DB::enableQueryLog();
$model->studentRollId = $value1;
$model->resident_permit_number = $value2;
$model->save()
dd(\DB::getQueryLog());
Try This
$model->studentRollId = $value1;
$model->resident_permit_number = $value2;
$data = $model->toSql();
dd($data);
Do you have studentRollId and resident_permit_number in the $fillable array?
Go to the $model and check if you have something like this:
protected $fillable = [
'studentRollId',
'resident_permit_number'
];
Another solution would be to insert data in raw like this:
DB::table('table_name')->insert([
'studentRollId' => $value1,
'resident_permit_number' => $value2
]);
Hope it helps.
Have you mentioned the column names in $fillable in you model. In laravel to insert a data in table you have to mentioned the columns names in $fillable method.
e.g.
Suppose you have users table and for that table you have User model.
In User.php add following method
protected $fillable = [
'list of columns'
];
According to Laravel's documentation:Inserts
Note: When using PostgreSQL the insertGetId method expects the auto-incrementing column to be named "id".
So, is there a workaround for a custom id name while using inserGetId. i.e.
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
You can use the Eloquent method Model::create() to return the object inserted, and obtain the object id, whatever his name is.
Try this:
$user = User::create(['email'=>'john#ecample.com','votes'=>0]);
$id = $user->custom_id;
This works if your User model is like:
class User extends Eloquent {
//This custom_id column needs to be of type serial in your table
protected $primaryKey = 'custom_id';
protected $fillable = ['email','votes'];
//...more code ...
}
I hope this works for you
I am using Laravel's fluent query builder to insert a row into a table and want to get the newly inserted row's id. For instance, I would normally do something like:
public static function next_id() {
$con = self::getInstance();
return (int)mysqli_insert_id($con);
}
Is there a simple solution?
Ok, just flicked through \laravel\database\query.php and on line 820, there is a function called insert_get_id($values, $column = 'id') where the second parameter appears to return any column, but id by default!
yup it's on the doc http://laravel.com/docs/database/fluent - inserting record part
$id = DB::table('table_name')->insertGetId(
array('email' => 'john#example.com', 'votes' => 0)); return $id;