正在编写一个测试,我需要使用Faker工厂构建器创建Eloquent模型Video
的新实例
$user = create(User::class);
$video = create(Video::class, 'make')->toArray(); // toArray serializes the model to include accessors
$user->videos()->create($video); // <--- Error occurs here
PDOException:SQLSTATE [HY000]:常规错误:1个表视频没有名为视图的列
create()
是自动加载文件中factory()
周围的辅助函数包装器
/**
* Generate a fake model
*
* Call the factory helper function on given model
*
* @param Illuminate\Database\Eloquent\Model $model Eloquent Model
* @param string $method create or make the model
* @param int $times How many model instances to return
* @param array $properties Model attributes to override in factory
*
* @return mixed Illuminate\Database\Eloquent\Model|array|collection
**/
function create($model, $method = 'create', $times = null, $properties = [])
{
return factory($model, $times)->$method($properties);
}
模型Video
附加了一个views
访问器(与数据库无关),这是模型
class Video extends BaseModel
{
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['views', 'length', 'timesReported'];
}
views
访问器在BaseModel
中,就是这样
class BaseModel extends Model
{
public $guarded = []; // Yolo!!
/**
* Get the user who owns the model.
*/
public function user()
{
return $this->belongsTo('App\User');
}
// Get model views count from Redis
public function getViewsAttribute()
{
return \Redis::zscore('popular_'.$this->getTable(), $this->id);
}
}
这里是VideoFactory
,以防万一
$factory->define(App\Video::class, function (Faker $faker) {
return [
'title' => $faker->realText(50, 2),
'uploader' => 'Unknown',
'duration' => '00:00:00',
'thumbnail' => $faker->imageUrl(),
'poster' => $faker->imageUrl(),
'slides' => $faker->imageUrl(),
'hls' => $faker->url,
'mp4' => $faker->url,
'_3gp' => $faker->url,
'quality' => $faker->randomElement(['normal', 'hd']),
];
});
以及videos
表迁移
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->json('title'); // MySQL doesn't allow uniqueness on json type columns
$table->enum('quality', ['normal', 'hd']);
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('uploader');
$table->time('duration');
$table->string('thumbnail')->unique();
$table->string('poster')->unique();
$table->string('slides')->unique();
$table->string('hls')->unique();
$table->string('mp4', 350)->unique();
$table->string('_3gp', 350)->unique();
$table->json('slug');
$table->timestamps();
});
我曾考虑使用array_except
,但是每次附加另一个访问器时,我都必须修改测试
答案 0 :(得分:1)
您可以在关系上使用save
代替create
,因为您已经拥有一个Video
实例,该实例具有您想要的工厂属性:
$user = create(User::class);
$video = create(Video::class, 'make');
$user->videos()->save($video);
通过这种方式,您无需拉出属性即可创建相同模型的新实例并对其进行调用(create
正在这样做)。
如果您确实想在该关系上使用create
,这意味着您需要将属性传递给create
,则可以调用getAttributes
而不是toArray()
:>
$video = create(Video::class, 'make')->getAttributes();
$user->videos()->create($video);
答案 1 :(得分:1)
我还发现工厂构建器具有raw
方法,该方法仅返回模型工厂中定义的数组
$user = create(User::class);
$video = create(Video::class, 'raw');
$user->videos()->create($video);
无需序列化,raw
已经是一个数组