我正试图通过保存多个记录
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()虽然根据我读过,该方法不会创建/更新时间戳.
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)
上面的问题是它不会更新时间戳,在这里找到示例
创建多个方法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");
}
}
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)
而且很有效,你也可以尝试一下。谢谢
如果您想在播种器中存储多个记录,请使用此方法,而不是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)