Insert Multiple Records At Once With Laravel

前端 未结 4 1268
栀梦
栀梦 2021-01-05 09:14

I am inserting the data to the rows one by one, but I have heard somewhere that it requires much time if there are many data to insert. So what are the ways of inserting the

4条回答
  •  伪装坚强ぢ
    2021-01-05 09:53

    public function add(Request $request)
      {
        if($request->ajax())
        {
           $books=$request->books;
           $data = array();
           foreach($books as $book)
           {
            if(!empty($book))
            {
              $data[] =[
                        'name' => $book,
                        'user_id' => Auth::id(),
                       ];                 
    
           }}
          Book::insert($data);
           
         }}
    

    make sure imported use Illuminate\Support\Facades\Auth;

提交回复
热议问题