【发布时间】:2021-07-28 15:11:04
【问题描述】:
我有两张桌子news 和images。这两个表具有一对多的关系(一个是新闻)。我正在尝试在图像上创建工厂,但在我使用种子迁移后,图像保存到新闻目录但一秒钟后它被删除,并且数据库上的路径返回news。我读过这个question 但是 laravel 8.4(我当前的 laravel 项目)使用 phpfaker,所以我猜它现在已经被弃用了。
ImageFactory.php 文件
<?php
namespace Database\Factories;
use App\Models\Image;
use Illuminate\Database\Eloquent\Factories\Factory;
class ImageFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Image::class;
/**
* State dari gambarnya,
* pilih satu [news, packages, destinations]
*/
public function news()
{
return $this->state(function (array $attributes) {
return [
'role' => 'news',
];
});
}
public function packages()
{
return $this->state(function (array $attributes) {
return [
'role' => 'packages',
];
});
}
public function destinations()
{
return $this->state(function (array $attributes) {
return [
'role' => 'destinations',
];
});
}
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'path' => 'news' . '/' . $this->faker->image(storage_path('app/public/news'), 2000, 1500, null, false),
];
}
}
storage/app/public 和 storage/app/public/news 具有 .gitignore 文件。我不知道这是否相关,因为我的同事迁移种子没有任何问题。
【问题讨论】: