Laravel 8 Multiple Image Upload and store in mysql database using Laravel 8 file system and Intervention Image package.
To upload and store multiple images in a MySQL database using Laravel 8, File System, and Intervention Image package, follow the steps below:
- Install Intervention Image package: First, you need to install the Intervention Image package using Composer. Run the following command in your terminal:
composer require intervention/image
- Create a migration file: Next, create a migration file to add an image column to your database table. Run the following command in your terminal:
php artisan make:migration add_images_to_posts_table --table=posts
In the newly created migration file, add the following code inside the up() method:
Schema::table('posts', function (Blueprint $table) {
$table->string('image_path')->nullable();
$table->string('image_path_1')->nullable();
$table->string('image_path_2')->nullable();
// Add more image columns as needed
});
Run the migration using the following command:
php artisan migrate
- Create a Form Request: Create a new form request to validate the multiple image uploads. Run the following command in your terminal:
php artisan make:request StorePostRequest
In the newly created StorePostRequest file, add the following code inside the rules() method:
'images' => 'required|array',
'images.*' => 'image|mimes:jpeg,png,jpg,bmp,svg,gif,pdf,png|max:2048',
- Update the controller: Update your PostController to handle the multiple image uploads. In the store() method, add the following code:
use App\Http\Requests\StorePostRequest;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
public function store(StorePostRequest $request)
{
$validated = $request->validated();
$post = Post::create($validated->all());
if ($request->hasFile('images')) {
foreach ($request->file('images') as $image) {
$imageName = time() . '_' . $image->getClientOriginalName();
$image->storeAs('public/uploads', $imageName);
$imagePath = Image::make($image)->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
})->encode();
$post->images()->create([
'path' => $imageName,
'size' => $imagePath->getSize(),
'mime_type' => $image->getMimeType(),
'extension' => $image->extension()
]);
}
}
return redirect()->back()->with('success', 'Post created successfully.');
}
- Update the route: Update your route file to use the StorePostRequest in the store() method. In web.php, add the following code:
Route::post('/posts', [PostController::class, 'store'])->middleware('auth')->middleware('csrf')->name('posts.store');
- Create a form: Finally, create a form in your view file to allow users to upload multiple images. In your create or edit form, add the following code:
<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="images[]" multiple>
<!-- Add other form fields as needed -->
<button type="submit">Create Post</button>
</form>
Now, when a user submits the form, multiple images will be uploaded and stored in the 'public/uploads' directory, and their metadata will be saved in the database.