简体   繁体   中英

get an id during model->save();

i have an query, lets say its looks like this :

$insert = new A;
$insert->col1 = $request['X'];
try {
$insert->save();
}

i wonder how to get the id of the query result when save() return true? i need the data id on $insert query, then i insert the value to a new query can i make it like this?

$insert = new A;
$insert->postname = $request['X'];
try {
$insert->save();
$counter = new B;
$counter->post_id = $insert->id;
$counter->post_name = $insert->postname;
}

sorry about my broken english.

for you guys who doesnt understand what i talking about, here is an example : i want to insert the id from query table A ($insert) to post_id on table B ($counter)

so, run the first query then get the id of the result then insert it to second query

how to do that in laravel 5.x ?

Thank you

The object will have the id after the save is completed. So you can get the id by doing

$id = $insert->id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM