Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 750 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving multiple records in a laravel eloquent create

#1
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.
Reply

#2
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.
Reply

#3
You can just use Eloquent::insert() [link][1] 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][2]

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
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.
Reply

#5
The Create many Method `createMany` is available on relationship check reference to this [link][1] and this [documentation][2] 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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#6
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); });
}
}

```
Reply

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

```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through