close

Saving multiple records in a laravel eloquent create

Hello Guys, How are you all? Hope You all Are Fine. Today we are talking about Saving multiple records in a laravel eloquent create in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Saving multiple records in a laravel eloquent create Error Occurs?

Today we are talking about Saving multiple records in a laravel eloquent create in php.

How To Solve Saving multiple records in a laravel eloquent create Error ?

  1. How To Solve Saving multiple records in a laravel eloquent create Error ?

    To Solve Saving multiple records in a laravel eloquent create Error The Create many Method createMany is available on relationship check reference to this link and this documentation from laravel

  2. Saving multiple records in a laravel eloquent create

    To Solve Saving multiple records in a laravel eloquent create Error The Create many Method createMany is available on relationship check reference to this link and this documentation from laravel

Solution 1

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.

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment