9

I need to store exactly three pages at once via form. I would like save in similar manner as model save() method, because this will automatically update record timestamps.

How to do this for multiple records at once?

My page Model:

namespace App;
use Illuminate\Database\Eloquent\Model;

class Page extends Model{
     protected $table = 'simple_pages';
}

My code:

public function createPages(Request $request){ // I use Page at the top
     $data = [
          [
          'title'=> $request->first,
          'content'=> $request->firstCont
          ],[
          'title'=> $request->second,
          'content'=> $request->secondCont
          ][
          'title'=> $request->third,
          'content'=> $request->thirdCont
          ]
     ];
     Page::unguard();
     $pages = new Page($data);
     $pages->save(); // Something like this would be amazing
     Page::reguard();
}

Note: I am strongly against creating multiple instances of Page model, and then Loop them to save them each individualy. Also, I dont want to use DB insert, because it will not update record timestamps automaticaly.

Fusion
  • 5,046
  • 5
  • 42
  • 51
  • 2
    I know this is an old question but the solution is `Page::insert($data)`. See this answer: https://stackoverflow.com/a/29723968/172790 – Soulriser Apr 05 '18 at 23:48

4 Answers4

5

After long long search looks like I found this:

        $eloquentCollection->each(function ($item, $key) {
            $item->save();
        });

Yes, it is Iteration, but looks like there are no other methods how to save multiple models. The only alternative is to use DB::insert(), but be aware of not having automaticaly updated timestamps.

Also, if you are willing to have too much eloquent models to save, save them by chunks because of possible memory issues (or push them into Queue).

Fusion
  • 5,046
  • 5
  • 42
  • 51
  • In newer Laravel-Versions (since version 5.4) you can shorten this to: $eloquentCollection->each->save(); – rissom Apr 11 '23 at 13:10
4

Here's the best solution I found to implement

User::create([array of multiple user detail arrays])

with avoid the need to executes as much queries as you have entries to add

Allow Eloquent to save multiple records at once

Mahmoud Nassar
  • 609
  • 7
  • 16
2

If you read the manual Mass Assignment.

public function createPages(Request $request){ // I use Page at the top
     $data = [
          [
          'title'=> $request->first,
          'content'=> $request->firstCont
          ],[
          'title'=> $request->second,
          'content'=> $request->secondCont
          ][
          'title'=> $request->third,
          'content'=> $request->thirdCont
          ]
     ];

     Page::unguard();
     $pages = Page::create($data);
     Page::reguard();
}
Ash
  • 3,242
  • 2
  • 23
  • 35
  • Thanks for answer. I already read official docs, but it provided no help to me. Page::create($data) creates exception(preg_match() expects parameter 2 to be string, array given); I also tried Page::create([ ... first ....],[ ... second ....],[ ... third ....]), but it just inserts single blank record to database, which is not what I want to achieve. – Fusion Apr 05 '16 at 20:09
  • But if your columns are not listed inside of `protected $fillable = []` it wont work. If you have set `$fillable` and your arrays are set, you need to make sure your values are actually strings and not arrays. i.e. `$request->first` and `$request->firstCont` – Ash Apr 05 '16 at 20:13
  • I used model unguard() and reguard() model methods. Therefore the guarded/fillable should not be an issue here (or is it? - If it would be an Issue, Laravel would throw mass assignment exception). I am 100% sure, about content of my requests. I recently checked with dd(). All of them are strings. – Fusion Apr 05 '16 at 20:22
  • @Fusion true `Unguard` would make `$fillable` irrelevant, missed that. Looking at [create](https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L567) it goes through the [__construct](https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L275) to [fill](https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L436) Maybe you can debug that a bit? – Ash Apr 05 '16 at 20:29
0
$data = [
      [
      'title'=> $request->first,
      'content'=> $request->firstCont
      ],[
      'title'=> $request->second,
      'content'=> $request->secondCont
      ][
      'title'=> $request->third,
      'content'=> $request->thirdCont
      ]
 ];

foreach($data as $page) {
      Page::create($page);
    }

should solve your issue

Julian
  • 129
  • 1
  • 7