在 laravel eloquent create 中保存多条记录

问题描述 投票:0回答:6

我正在尝试通过

保存多条记录
AppSettings::create(
    [
        'name' => 'mail_host',
        'type' => $emailsettingstype->id,
        'value' => '',

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

但是从上面来看,只有第一个数组被创建。我哪里出错了?如有任何帮助,我们将不胜感激。

php laravel
6个回答
30
投票

我认为这应该可以(更新:自 Laravel 8 以来不再可能

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

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

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

确保您传递的是数组的数组,而不是数组的参数

更新,您可以使用

Model::insert()
,尽管根据我所读到的内容,该方法不会创建/更新时间戳。


25
投票

您可以使用 Eloquent::insert() 链接,如下所示:

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

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

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

上面的问题是它不会更新时间戳,找到示例这里


6
投票

创建多个方法

createMany
可在关系检查中使用,参考此链接和来自laravel的文档

到目前为止我的例子看起来像这样。

我有两个型号

Pricing
AvailableService
型号

定价模型

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

AvailableServiceMode
看起来像这样

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

所以

createMany
操作看起来像这样

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

而且很有效,你也可以尝试一下。谢谢


5
投票

如果您想在播种器中存储多个记录,请使用此方法而不是

insert
,因为在我的情况下,我想使用
spatie/laravel-sluggable
pkg 自动创建的 slug。如果您使用
insert
DB
技术,那么您还必须给出
slug
字段的值。

类别播种者

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


0
投票

万一有人寻找雄辩模型,我使用了以下方法:

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();
是必须的,否则使用
$this
时只会保存单个记录。


-2
投票

在播种器中创建一个数组并使用 Model::create() 执行 foreach。您的所有记录都会带有时间戳


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

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

© www.soinside.com 2019 - 2024. All rights reserved.