gpt4 book ai didi

php - Laravel:有几个种子表属于使用工厂的关系

转载 作者:行者123 更新时间:2023-12-02 19:28:40 24 4
gpt4 key购买 nike

tl;dr:

我正在使用 Laravel 和 Eloquent 并尝试为数据库播种并将多个属于关系附加到一个表,但我收到错误:

Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::attach()

详细解释:

我在 Eloquent 中使用 php 7.1Laravel 6。我正在尝试为测试数据库播种。我有以下关系结构:

一个用户有多个帖子/一个帖子属于一个用户

一个类别有多个帖子/一个帖子属于一个类别

用户模型:

class User extends Model
{
protected $table = 'users';

public function posts()
{
return $this->hasMany('App\Models\Post');
}
}

类别模型:

class Category extends Model
{
protected $table = 'categories';

public function posts()
{
return $this->hasMany('App\Models\Category');
}
}

后模型:

class Post extends Model
{
protected $table = 'posts';

public function category()
{
return $this->belongsTo('App\Models\Category');
}

public function user()
{
return $this->belongsTo('App\Models\User');
}
}

用户工厂:

$factory->define(User::class, function (Faker $faker) {
return [
'id' => $faker->unique()->randomNumber(3),
'name' => $faker->name(),
];
});

类别工厂:

$factory->define(Category::class, function (Faker $faker) {
return [
'id' => $faker->unique()->randomNumber(3),
'name' => $faker->word(),
];
});

邮局:

$factory->define(User::class, function (Faker $faker) {
return [
'id' => $faker->unique()->randomNumber(3),
'name' => $faker->realText($maxNbChars = 200, $indexSize = 2),
];
});

如果我只有用户和帖子,我知道我可以通过以下方式为数据库播种:

public function seed()
{
$users = factory(App\User::class, 10)
->create()
->each(function ($user) {
$user->posts()->createMany(factory(App\Post::class, 10)->make()->toArray());
});
}

但显然这对我来说行不通。我尝试了以下方法:

public function seed()
{
factory(User::class, 10)->create();
factory(Category::class, 10)->create();
factory(Post::class, 100)->create()->each(function ($post){
$post->user()->attach(User::all()->random(1));
$post->category()->attach(Category::all()->random(1));
});
}

以及使用 make/save 而不是 create 的相同功能:

public function seed()
{
factory(User::class, 10)->create();
factory(Category::class, 10)->create();
factory(Post::class, 100)->make()->each(function ($post) {
$post->user()->attach(User::all()->random(1));
$post->category()->attach(Category::all()->random(1));
})->save();
}

但在这两种情况下,我都得到了错误:

Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::attach()

最佳答案

你必须使用associate():

public function seed()
{
factory(User::class, 10)->create();
factory(Category::class, 10)->create();
factory(Post::class, 100)->make()->each(function ($post) {
$post->user()->associate(User::inRandomOrder()->first());
$post->category()->associate(Category::inRandomOrder()->first());
$post->save();
});
}

此外 User::all()->random(1) 将返回一个集合而不是模型,这将引发异常,我将其替换为 Model::inRandomOrder( )->first(),它将从数据库中获取一个随机模型。

来自docs :

When updating a belongsTo relationship, you may use the associate method. This method will set the foreign key on the child model:

$account = App\Account::find(10);

$user->account()->associate($account);

$user->save();

更新

在您的模型工厂中:

/** @var Factory $factory */
$factory->define(Post::class, function (Faker\Generator $faker) {
return [
// not sure why you do this, is it not a autoincrement column?
'id' => $faker->unique()->randomNumber(3),
'name' => $faker->realText($maxNbChars = 200, $indexSize = 2),
'category_id' => function () {
if ($category = Category::inRandomOrder()->first()) {
return $category->id;
}

return factory(Category::class)->create()->id;
},
'user_id' => function () {
if ($user = User::inRandomOrder()->first()) {
return $user->id;
}

return factory(User::class)->create()->id;
},
];
});

在你的播种机中:

public function seed()
{
factory(User::class, 10)->create();
factory(Category::class, 10)->create();
factory(Post::class, 100)->create();
}

关于php - Laravel:有几个种子表属于使用工厂的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62148552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com