Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

File Uploading, Seeders, and Faker in Laravel

Upload files to storage, generate fake demo data with factories/Faker, and seed the database.

File Uploading, Seeders, and Faker in Laravel — a practical guide to Laravel file upload seeder faker with clear examples you can reuse in real projects.

Laravel Tutorial Series (18/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Combine file uploads for user content with seeders/factories to fill development databases quickly.

File upload example

public function store(Request $request)
{
    $request->validate([
        'avatar' => ['required', 'image', 'max:2048'],
    ]);

    $path = $request->file('avatar')->store('avatars', 'public');

    auth()->user()->update(['avatar_path' => $path]);

    return back()->with('success', 'Uploaded');
}

Show uploaded file

<img src="{{ asset('storage/'.$user->avatar_path) }}" alt="Avatar">
php artisan storage:link

Factory + Faker

php artisan make:factory PostFactory --model=Post
public function definition(): array
{
    return [
        'title' => fake()->sentence(),
        'body'  => fake()->paragraphs(3, true),
    ];
}

Seeder

php artisan make:seeder PostSeeder
Post::factory()->count(50)->create();
php artisan db:seed --class=PostSeeder

Leave a reply

Your email address will not be published. Required fields are marked *