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.

1

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

In newer Laravel-Versions (since version 5.4) you can shorten this to: $eloquentCollection->each->save();
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

Comments

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();
}

4 Comments

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.
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
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 true Unguard would make $fillable irrelevant, missed that. Looking at create it goes through the __construct to fill Maybe you can debug that a bit?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.