如何在 laravel 中为表格播种与其自身的关系?
我有这个cows表迁移:
Schema::create('cows', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable(false);
$table->char('gender',1)->nullable(false);
$table->unsignedBigInteger('cow_father_id')->nullable();
$table->foreign('cow_father_id')->references('id')->on('cows')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedBigInteger('cow_mother_id')->nullable();
$table->foreign('cow_mother_id')->references('id')->on('cows')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
在我的Cow模型中:
protected $guarded = [];
public function father()
{
return $this->belongsTo(self::class, 'cow_father_id');
}
public function mother()
{
return $this->belongsTo(self::class, 'cow_mother_id');
}
public function children()
{
return $this->hasMany(self::class, 'cow_father_id')->orWhere('cow_mother_id', $this->id);
}
考虑到它与自身的关系,该表的工厂和数据库播种器是什么?
确保母亲的性别为女性F,父亲的性别为男性M。
我自己取得的成果并不能满足我的需求,这是我的CowFactory。
use App\Cow;
use Faker\Generator as Faker;
Cow::create([
'name' => 'adam',
'gender' => 'M'
]);
Cow::create([
'name' => 'eve',
'gender' => 'F'
]);
$factory->define(Cow::class, function (Faker $faker) {
return [
'name' => $faker->name,
'gender' => $faker->randomElement(['M', 'F']),
'cow_father_id' => Cow::where('gender','M')->get()->random()->id,//Cow::all()->random()->id,
'cow_mother_id' => Cow::where('gender','F')->get()->random()->id,//Cow::all()->random()->id,
];
});
正如你所看到的,我创造了一个没有父亲或母亲的亚当和夏娃。
这就是我的 DatabaseSeeder 中的内容。
public function run()
{
factory(Cow::class, 20)->create()->each(function($c) {
dump($c->father()->first()->name.','.$c->mother()->first()->name);
//$c->mother()->save(factory(Cow::class)->make());
//$c->father()->save(factory(Cow::class)->make());
});
}
我可以打印指定的父亲和母亲的名字,但我无法实现自动生成的奶牛可以是其他奶牛的父亲或母亲,我只能实现所有奶牛都有相同的父母。
我想要一个 DatabaseSeed 来生成这些具有不同父亲和母亲的奶牛。可以重复(几头牛有相同的父亲和母亲,但不是全部),甚至这些可以为空。
但是如果我加载父亲或母亲的 ID,如果它存在于我的数据库中。
i have this cows table migration:
Schema::create('cows', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable(false);
$table->char('gender',1)->nullable(false);
$table->unsignedBigInteger('cow_father_id')->nullable();
$table->foreign('cow_father_id')->references('id')->on('cows')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedBigInteger('cow_mother_id')->nullable();
$table->foreign('cow_mother_id')->references('id')->on('cows')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
and this in my Cow model:
protected $guarded = [];
public function father()
{
return $this->belongsTo(self::class, 'cow_father_id');
}
public function mother()
{
return $this->belongsTo(self::class, 'cow_mother_id');
}
public function children()
{
return $this->hasMany(self::class, 'cow_father_id')->orWhere('cow_mother_id', $this->id);
}
what would be the factory and database seeder for this table, taking into consideration its relationship with itself?
ensuring that the gender of the mothers is female F and the gender of the fathers is male M.
what I have achieved on my own does not meet my needs, this is my CowFactory.
use App\Cow;
use Faker\Generator as Faker;
Cow::create([
'name' => 'adam',
'gender' => 'M'
]);
Cow::create([
'name' => 'eve',
'gender' => 'F'
]);
$factory->define(Cow::class, function (Faker $faker) {
return [
'name' => $faker->name,
'gender' => $faker->randomElement(['M', 'F']),
'cow_father_id' => Cow::where('gender','M')->get()->random()->id,//Cow::all()->random()->id,
'cow_mother_id' => Cow::where('gender','F')->get()->random()->id,//Cow::all()->random()->id,
];
});
as you can see, I have generated an adam and an eve without father or mother.
and this is what is in my DatabaseSeeder.
public function run()
{
factory(Cow::class, 20)->create()->each(function($c) {
dump($c->father()->first()->name.','.$c->mother()->first()->name);
//$c->mother()->save(factory(Cow::class)->make());
//$c->father()->save(factory(Cow::class)->make());
});
}
I can print the name of the assigned father and mother, but I can not achieve that in an automatic way the cows that are generated can be fathers or mothers of the other cows, I can only achieve that all of them have the same parents.
I want a DatabaseSeed that generates these cows with different fathers and mothers. that can be repeated (several cows with the same father and mother, but not all) and even that these can be null.
but if I load an id of father or mother that this if it exists in my DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论