LoginSignup
0
3

More than 5 years have passed since last update.

LaravelでhasManyの関係あるテーブルにsaveManyで一括挿入

Posted at
$post = App\Post::find(1);

for($i = 0; $i < count($request->comments); $i++) {
    $comments[] = new App\Post([
        'name'      => $request->name[$i],
        'comment'   => $request->comment[$i] 
    ]);
}

$post->comments()->saveMany($comments);
0
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3