Nov 5, 2020
Laravel-medialibrary is a Laravel package for associating all sorts of files with Eloquent models.
We have an Auction
model that uses the library to associate an image with each object,
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Auction extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
}
We wanted to seed an Eloquent model that has associated images using a seeder AuctionSeeder
:
class AuctionSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Auction::factory()
->count(10)
->create();
}
}
running a factory AuctionFactory
,
namespace Database\Factories;
use App\Models\Auction;
use Illuminate\Database\Eloquent\Factories\Factory;
class AuctionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Auction::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => Str::substr($this->faker->words(5, true), 0, 30)…