简体   繁体   中英

Laravel - Inserting Related Models

I am developing a system in Laravel 6. The system inserts a row into a header table and in the same operation needs to insert related details into a detail table. Is there a better way of doing this than the below:

$header = new RequestHeader;
$insert = $header->create($data)->id;

$meta = RequestHeader::find($insert);
$meta->details()->createMany($data['module']);
$meta->status()->create(['status' => $initialStatus, 'action_by' => \Auth::user()->username]);```

Try below because, you have modal object so, no need to apply query to find it out.

$header = new RequestHeader;
$meta = $header->create($data);

$meta->details()->createMany($data['module']);
$meta->status()->create(['status' => $initialStatus, 'action_by' => \Auth::user()->username]);

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