Here are the key steps to handle file uploads in a Laravel CRUD application:
Setting up the Database Migration
1. Create a migration for the table that will store the file uploads:
bash
php artisan make:migration create_posts_table --create=posts
2. In the migration file, add a column for the file path:
php
$table->string('image');
Creating the Model and Controller
3. Generate a model and controller with resource methods:
bash
php artisan make:model Post -mc --resource
4. In the controller's `store` method, handle the file upload:
php
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'image' => 'required|image|max:2048',
]);
$imageName = time() . '.' . $request->image->extension();
$request->image->move(public_path('images'), $imageName);
$post = Post::create([
'title' => $request->title,
'image' => $imageName,
]);
return redirect()->route('posts.index')
->with('success', 'Post created successfully.');
}
5. In the controller's `update` method, handle file updates:
php
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'image' => 'image|max:2048',
]);
if ($request->hasFile('image')) {
$imageName = time() . '.' . $request->image->extension();
$request->image->move(public_path('images'), $imageName);
$post->image = $imageName;
}
$post->title = $request->title;
$post->save();
return redirect()->route('posts.index')
->with('success', 'Post updated successfully');
}
Creating the Views
6. Create the necessary views (e.g., `index.blade.php`, `create.blade.php`, `edit.blade.php`) and include file input fields:
html
<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" class="form-control-file" id="image" name="image" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
7. In the `index.blade.php` view, display the uploaded images:
html
@foreach ($posts as $post)
<div class="card" style="width: 18rem;">
<img src="{{ asset('images/' . $post->image) }}" class="card-img-top" alt="{{ $post->title }}">
<div class="card-body">
<h5 class="card-title">{{ $post->title }}</h5>
<a href="{{ route('posts.edit', $post->id) }}" class="btn btn-primary">Edit</a>
</div>
</div>
@endforeach
By following these steps, you can successfully handle file uploads in your Laravel CRUD application. Remember to configure the appropriate file storage settings in your application's configuration files.
Citations:[1] https://www.souysoeng.com/2023/06/laravel-crud-operation-with-file-upload.html
[2] https://www.webslesson.info/2019/03/step-by-step-crud-operation-in-laravel-58-with-file-upload.html
[3] https://www.itsolutionstuff.com/post/crud-with-image-upload-in-laravel-8-exampleexample.html
[4] https://dcodemania.com/post/crud-application-image-upload-laravel-8
[5] https://www.youtube.com/watch?v=MOXMzNuQRFE