在laravel eloquent创建中保存多个记录

Geo*_*off 8 php laravel

我正试图通过保存多个记录

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

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

    ],
    [
        'name'=>'mail_username',
        'type'=>$emailsettingstype->id,
        'value'=>'',
    ],
);
Run Code Online (Sandbox Code Playgroud)

但是从上面开始,只创建了第一个数组.我哪里错了?任何帮助表示赞赏.

Wre*_*igh 12

我认为应该这样做

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

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

    ],
    [
        'name'=>'mail_username',
        'type'=>$emailsettingstype->id,
        'value'=>'',
    ],
]);
Run Code Online (Sandbox Code Playgroud)

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

更新,您可以使用Model::insert()虽然根据我读过,该方法不会创建/更新时间戳.

  • 调用未定义的方法 App\Models\Post::createMany() ,laravel 版本 8 (11认同)
  • 这个答案适用于 Laravel 的早期版本,不幸的是,我不再是最新的,并且不确定在最新的主要版本中应该如何完成此操作。如果有人真的更新这个,如果这增加了任何价值,我将不胜感激。 (4认同)
  • https://laravel.com/docs/9.x/eloquent-relationships#the-create-method 此建议不适用于版本 9 或任何以前的 Laravel 版本。根据 Laravel 官方文档,方法 createMany() 确实“创建多个相关模型”,这意味着它仅适用于相关模型,而不是直接适用于模型,如上所述。 (4认同)

Gan*_*ame 10

您可以使用 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'=>'',
    ],
]);
Run Code Online (Sandbox Code Playgroud)

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

  • 注意:“Model::insert()”不存储“created_at”和“updated_at”,因此如果需要,您应该自己添加它们。 (7认同)

Rub*_*nce 5

创建多个方法createMany可在关系检查参考此链接和来自 laravel 的文档中找到

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

我有两个模型PricingAvailableService模型

定价模型

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

并且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");
    }
}
Run Code Online (Sandbox Code Playgroud)

所以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],
]);
Run Code Online (Sandbox Code Playgroud)

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


Nee*_*iya 5

如果您想在播种器中存储多个记录,请使用此方法,而不是insert因为在我的情况下,我想使用spatie/laravel-sluggablepkg 自动创建 slug。如果您使用insertorDB技术,那么您还必须给出字段的值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); });
    }
}

Run Code Online (Sandbox Code Playgroud)