[英]how to seed one to many relationship in laravel 7
I have this migrations and I want to see them using a factory model我有这个迁移,我想看到他们使用工厂 model
migration category:迁移类别:
Schema::create('categories', function (Blueprint $table) {
$table->id('id_cat');
$table->string('nom');
});
migration marque:迁移品牌:
Schema::create('marques', function (Blueprint $table) {
$table->id('marque_id');
$table->foreignId('cat_id')->constrained();
$table->string('designation');
});
when I run this factory model it doesnt work当我运行这个工厂 model 它不起作用
category factory:类别工厂:
$factory->define(Category::class, function (Faker $faker) {
return [
'nom' => $faker->unique()->word
];
}); });
marque factory:品牌工厂:
$factory->define(Marque::class, function (Faker $faker) {
return [
'cat_id' => Category::all()->random()->id,
'designation'=>$faker->unique()->word
];
}); });
You have to make sure that your model property fillable mass assignment array is fill.您必须确保您的 model 属性可填充质量分配数组已填充。
example for Category::class
:例如
Category::class
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['nom'];
}
You have to do the same for your other class mass assignment property.您必须对其他 class 质量分配属性执行相同操作。 To get more information about read this .
要获取有关阅读此内容的更多信息。 Good luck and happy coding!
祝你好运,编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.