Steve
Steve

Reputation: 1672

Insert Multiple Records At Once With Laravel

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 them all at once?

public function add(Request $request)
{
    if ($request->ajax()) {
        $books = $request->books;
        foreach ($books as $book) {
            if (!empty($book)) {
                $add = new Book;
                $add->name = $book;
                $add->user_id = Auth::user()->id;
                $add->save();
            }
        }
    }
}

Upvotes: 17

Views: 64436

Answers (5)

Akintan Robert
Akintan Robert

Reputation: 1

foreach($request->refereename as $i =>$value)
  {
    $referees[]=[
        'refereename'=>$request->refereename[$i],
        'refereecompany'=>$request->refereecompany[$i],
        'refereephone'=>$request->refereephone[$i],
        'refereeemail'=>$request->refereeemail[$i],
        'relationship'=>$request->relationship[$i],
        'refereelocation'=>$request->refereelocation[$i],
        'created_at'=>$date,
        'updated_at'=>$date,
        ];
  }

  DB::table('db_referees')->insert($referees); // Query Builder approach 

Upvotes: -1

Qevo
Qevo

Reputation: 2371

Insert multiple records using the Model

As others have pointed out, using the Query Builder is the only way to insert multiple records at a time. Fortunately Laravel and the Eloquent ORM are coupled in many useful ways. This coupling allows you to use a Model to get a Query Builder instance that is set for that Model.

// use Auth;
// use Carbon;
// use App\Book;

public function add(Request $request)
{
    if($request->ajax())
    {
        // Submitted books
        $books = $request->books;

        // Book records to be saved
        $book_records = [];

        // Add needed information to book records
        foreach($books as $book)
        {
            if(! empty($book))
            {
                // Get the current time
                $now = Carbon::now();

                // Formulate record that will be saved
                $book_records[] = [
                    'name' => $book,
                    'user_id' => Auth::user()->id,
                    'updated_at' => $now,  // remove if not using timestamps
                    'created_at' => $now   // remove if not using timestamps
                ];
            }
        }

        // Insert book records
        Book::insert($book_records);
    }
}

Upvotes: 11

Hamelraj
Hamelraj

Reputation: 4826

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);
       <!--DB::table('books')->insert($data);-->
     }}

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

Upvotes: 31

Bogdan Koliesnik
Bogdan Koliesnik

Reputation: 900

If you need Eloquent model events - there is no other way to insert multiple models. In other way - check Anushan W answer

Upvotes: -3

Shamintha
Shamintha

Reputation: 89

You should be able to do something like below:

DB::table('users')->insert([
    ['email' => '[email protected]', 'votes' => 0],
    ['email' => '[email protected]', 'votes' => 0]
]);

Put all the values you want to insert in to an array and then pass it to the insert function.

Source: https://laravel.com/docs/5.1/queries#inserts

Upvotes: 9

Related Questions