How to Upload Multiple Images in Laravel

Paul Nasilele
Geek Culture
Published in
3 min readAug 30, 2021

--

Image by positronx.io

A good number of websites require the user to upload multiple images of a product. This especially applies to e-commerce sites. Multiple images are used to provide a lot of details about the product on sale.

Implementing multiple image uploads has proven to be difficult for many developers. In this article, I will show you step by step how to upload multiple images using laravel.

Step 1: Create a new Laravel project using composer. You can give it any name but in this case, we will name it multipleImagesApp.

composer create-project --prefer-dist laravel/laravel muiltipleImagesApp

Step 2: In the multipleImageApp project create a migration to create a products table.

cd multipleImagesApp
php artisan make:migration create_products_table --create=products

Step 3: In the migration add the name and the description fields.

<?phpuse Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}

Step 4: Create another migration that will create the images.

php artisan make:migration create_images_table --create=images

Step 5: In the migration add the fields url and product_id.

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateImagesTable extends Migration
{
public function up(){
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('url');
$table->unsignedBigInteger('product_id');
$table->timestamps();
});
}
public function down(){
Schema::dropIfExists('images');
}}

Step 6: Next, create two models. One will be called Product and the other will be called Image.

php artisan make:model Product
php artisan make:model Image

Step 7: In the Product model add the title and content fields in the fillable array. Next, create a one-to-many relationship with the Image model.

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Product extends Model
{
protected $table = 'products';
protected $fillable = [
'name', 'discription'
];
public function images()
{
return $this->hasMany('App\Image', 'product_id');
}
}

Step 8: In the Image model add the url and the product_id fields in the fillable array. Next, create a one-to-many relationship with the Product model.

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Image extends Model
{
protected $table = 'images';
protected $fillable = [
'url', 'product_id'
];
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
}

Step 9: In the config/ folder, add a new disk called my_files in a file called filesystems.php.

'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
'my_files' => [
'driver' => 'local',
'root' => public_path() . '/'
]
],

Step 10: Next, create a controller and name it ProductController.

php artisan make:controller ProductController

Step 11: To the ProductController, add a function called uploadImages.

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Product;
use Image;
use Storage;
class ProductController extends Controller
{
public function addProduct(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'description' => 'required|string|max:855',
]);
$product = new Product;
$product->name = $request->name;
$product->description = $request->description;
$product->save();
foreach ($request->file('images') as $imagefile) { $image = new Image;
$path = $imagefile->store('/images/resource', ['disk' => 'my_files']);
$image->url = $path;
$image->product_id = $product->id;
$image->save();
}}}

Step 12: Finally in the web.php file in the routes folder

<?phpuse Illuminate\Http\Request;Route::post('addProduct', 'ProductController@addProduct')->name('products.uploadproduct');

Step 13: Create a file called uploadImages.blade.php.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Multiple Image upload</title>
</head>
<body><div>
<h3>Upload a Images</h3>
<hr>
<form method="POST" action="{{ route('products.uploadproducts') }}" enctype="multipart/form-data" >
{{ csrf_field() }}
<div >
<label>Name</label>
<input type="text" name="name" placeholder="Enter Product Name">
<label>Discription</label>
<textarea name="description" rows="4" >
</textarea></div>
<div >
<label>Choose Images</label>
<input type="file" name="images" multiple>
</div>
<hr><button type="submit" >Submit</button></form></div></body></html>

And there we have it. I hope you have found this useful. I will be back with more interesting articles. Thank you for reading.

--

--