21

I'm trying to save multiple records via

AppSettings::create(
    [
        'name' => 'mail_host',
        'type' => $emailsettingstype->id,
        'value' => '',

    ],
    [
        'name' => 'mail_port',
        'type' => $emailsettingstype->id,
        'value' => '',    
    ],
    [
        'name' => 'mail_username',
        'type' => $emailsettingstype->id,
        'value' => '',
    ],
);

But from the above, only the first array is getting created. Where am i going wrong? Any help is appreciated.

Maik Lowrey
  • 10,972
  • 4
  • 14
  • 43
Geoff
  • 5,349
  • 18
  • 74
  • 175

6 Answers6

22

I think this should do

AppSettings::createMany([
    [
        'name'=>'mail_host',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_port',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_username',
        'type'=>$emailsettingstype->id,
        'value'=>'',
    ],
]);

Make sure you're passing an array of arrays, not a params of array.

UPDATE, you can use Model::insert() although according to what I've read, that method doesn't create/update the timestamps.

Wreigh
  • 3,007
  • 10
  • 34
  • 1
    This answer was for an earlier version of Laravel, unfortunately, I'm no longer up to date and not sure how this should be done in the latest major version. Id appreciate if someone would actually update this, if that adds any value though. – Wreigh Nov 03 '20 at 01:10
  • 3
    Call to undefined method App\Models\Post::createMany() , Version 8 of laravel – Waleed Khaled Sep 19 '21 at 18:40
  • Model::insert() works on Laravel 7 – user3613026 Oct 06 '21 at 13:57
14

You can just use Eloquent::insert() link as below:

AppSettings::insert([
    [
        'name'=>'mail_host',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_port',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_username',
        'type'=>$emailsettingstype->id,
        'value'=>'',
    ],
]);

The problem with above is that it won't update timestamps, find examples here

Ganesh Ghalame
  • 4,937
  • 3
  • 23
  • 28
4

The Create many Method createMany is available on relationship check reference to this link and this documentation from laravel

so far my example look like this.

I have two models Pricing and AvailableService Model

Pricing Model

namespace App;
use Illuminate\Database\Eloquent\Model;
class Pricing extends Model
{
    protected $fillable = ["name", "price"];
    public function available(){
        return $this->hasMany(AvailableService::class, "pricing_id", "id");
    }
}

And the AvailableServiceMode look like this

namespace App;
use Illuminate\Database\Eloquent\Model;
class AvailableService extends Model
{
    protected $fillable = ["pricing_id", "service_id"];
    public function service(){
        return $this->belongsTo(Service::class, "service_id", "id");
    }
}

So createMany operation look like this

$insertMany = Pricing::create(['name'=>request('name')]);
$insertMany->available()->createMany([
      ['service_id'=>1],
      ['service_id'=>2],
      ['service_id'=>3],
      ['service_id'=>4],
      ['service_id'=>5],
]);

And it works for, you can give it a try too. THANKS

Karim Pazoki
  • 847
  • 10
  • 32
Ruberandinda Patience
  • 2,745
  • 3
  • 16
  • 16
3

If you want to store multiple record in seeder use this method instead of insert because in my case I want to slug automatically created using spatie/laravel-sluggable pkg. If you used the insert or DB technique then you have to give the value for slug field also.

CategorySeeder

<?php

namespace Database\Seeders;

use App\Servcategory;
use Illuminate\Database\Seeder;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $categories = [
            [
                'name' => 'Automotive',
                // 'slug' => 'automotive',
            ],
            [
                'name' => 'Business Services',
                // 'slug' => 'business-services',
            ],
            [
                'name' => 'Computer, Telecom & IT Services',
                // 'slug' => 'computer-telecom-&-it-services',
            ],
            [
                'name' => 'Education & Training',
                // 'slug' => 'education-&-training',
            ],
            [
                'name' => 'Finance',
                // 'slug' => 'finance',
            ],
            [
                'name' => 'Hospitals, Clinic, Medical',
                // 'slug' => 'hospitals-clinic-medical',
            ],
            [
                'name' => 'Real Estate, Construction, Property',
                // 'slug' => 'real-estate-construction-property',
            ],
            [
                'name' => 'Travel,Toursim & Hotels',
                // 'slug' => 'travel-toursim-&-hotels',
            ],
        ];

        // Servcategory::insert($categories);
        collect($categories)->each(function ($category) { Servcategory::create($category); });
    }
}

Neeraj Tangariya
  • 730
  • 1
  • 12
  • 23
0

In case some one searching for eloquent model, I used the following method:

foreach($arCategories as $v)
{                
    if($v>0){
        $obj = new Self(); // this is to have new instance of own
        $obj->page_id = $page_id;
        $obj->category_id = $v;
        $obj->save();
    }
}

$obj = new Self(); is a must otherwise it only saves single record when $this is used.

dazed-and-confused
  • 1,278
  • 2
  • 11
  • 18
justnajm
  • 4,045
  • 6
  • 33
  • 55
0

in seeder create an array and do foreach with Model::create(). All your records will be with timestamps


protected $array = [
    [...],
    [...],
    [...]
];

public function run()
{
    foreach ($this->array as $value) {
        Model::create($value);
    }
}

nurzzzone
  • 13
  • 3