小智 7
下面的教程展示了如何在播种器中添加图像。 https://www.5balloons.info/faker-images-in-laravel/#
使用以下方法: 'image' => $faker->image('public/storage/images',640,480, null, false),
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Product;
use Faker\Generator as Faker;
$factory->define(Product::class, function (Faker $faker) {
return [
'name' => $faker->word,
'short_description' => $faker->sentence,
'description' => $faker->paragraph,
'category_id' => function () {
return factory(App\Category::class)->create()->id;
},
'amount' => $faker->randomFloat(2, 0, 10000),
'image' => $faker->image('public/storage/images',640,480, null, false),
];
});
Run Code Online (Sandbox Code Playgroud)
如果您只想使用虚拟图像查看主页“实时”而不实际上传图像,您可以使用此站点显示随机图像:
<img src="http://lorempixel.com/400/200/sports/" />
您还可以在播种时将该图像路径存储在数据库中。
谢谢
在工厂方法中,在播种时,做这样的事情。
$factory->define(Post::class, function (Faker $faker) {
return [
'image'=>'https://source.unsplash.com/random',
];
});
Run Code Online (Sandbox Code Playgroud)
现在,每次运行工厂播种时,它都会将该链接放入图像字段中。此链接每次都会从 unsplash 中为您提供随机图像。
只需在 URL 后添加照片尺寸(例如:/800x600)
| 归档时间: |
|
| 查看次数: |
16542 次 |
| 最近记录: |