laravel-seeding - Laravel 8 数据库工厂
问题描述
我想在使用工厂时在播种机中定义工厂中的覆盖属性。
例如,在 Laravel 7 中,可以将它们作为第三个参数
$factory->define(Menu::class, function (Faker $faker, $params) {
/* here params have the override attributes, which can be used to specify other attributes based on it's value, for example menu_type */
}
现在升级到 laravel 8 时,是否有办法在定义方法中获取这些属性?
任何想法都会有所帮助。谢谢!
解决方案
class ArticleFactory extends Factory {
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Article::class;
/**
* Define the model's default state.
*
* @return array
*/
//
public function definition() {
return [
'user_id' => function(){
return User::factory()->create()->id;
},
'title' => $this->faker->title,
'body' => $this->faker->sentence,
];
}
}
推荐阅读
- json - 飞镖中的jsondecode加载问题
- google-api - 在最新的 Google Cloud SDK 291.0.1 中无法通过 REST API 获取 access_token 信息
- r - 在 R studio 中加载 keras 包时出错
- uwp - 如何将传输控制按钮添加到任务栏预览窗口
- python - llvmlitelandingpad throw RuntimeError:LandingPadInst 需要在一个有个性的函数中?
- python-3.x - 仅显示 matplotlib 条形图的一些条形标签
- git - git log 以图形方式显示 2 个分支上的所有提交,但仅显示最近的共享提交
- c++ - 是否为 c/c++ 中的块(条件或无条件)代码分配了新的堆栈帧?
- arrays - bash:存储/更新文本数据
- flutter - Flutter中如何实现字体大小调整对话框?