Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

usman9023's avatar

How Can I Upload Multiple Images at the same time ?

I want to upload multiple images at the same time . here is my postcontroller in which im uploading a single image.

public function store(Request $request) { $data = request()->validate([

        'PostCaption' => 'required',
        'image' => 'required|image ',
       
    ]);

    $imagepath = request('image')->store('uploads', 'public');
    $image = Image::make(public_path("storage/{$imagepath}"))->fit(1200, 1200);
    $image->save();


    post::create([
        'user_id' => auth()->user()->id,
        'Caption' => $data['PostCaption'],
        'image' => $imagepath,
    ]);
   
    return redirect('/dashboard/' . auth()->user()->id);
}

Any Suggestion how can i add multiple images at the same time ? Im new to this thanks in advance

0 likes
32 replies
usman9023's avatar

@Sinnbeck like this ? and where i use foreach ?

    $data = request()->validate([
        'PostCaption' => 'required',
        'images' => 'array|min:3',
        'images.*' => 'image ',
        'image' => 'required|image ',
       
    ]);

Here is my create.blade.php file

@section('content') @csrf
        <div class="row">
            <div class="col-8 offset-2">

                @csrf

                <div>
                    <x-input-label for="PostCaption" :value="__('Post Caption')" />

                    <x-text-input id="PostCaption" class="block mt-1 w-full" type="text" name="PostCaption"
                        :value="old('PostCaption')" />

                    <x-input-error :messages="$errors->get('PostCaption')" class="mt-2" />
                </div>

                <div class="row mb-3">

                    <x-input-label for="image" :value="__('Image')" />

                    <input type="file" , class=" form-control-file ml-3 mt-2" id="image" name=" image">
                    <x-input-error :messages="$errors->get('image')" class="mt-2" />
                </div>




                <div class="pt-4">
                    <button class=" btn btn-primary inline-flex">Submit</button>
                </div>


            </div>
        </div>



    </form>






</div>
@endsection
AungHtetPaing__'s avatar

@usman9023 use foreach in controller. and update input file.

<input type="file" class=" form-control-file ml-3 mt-2" id="image" name="image" multiple>

check the link given by @sinnbeck

usman9023's avatar

@AungHtetPaing__ POst Controller :

public function store(Request $request) {

    $data = request()->validate([

        'PostCaption' => 'required',
        'images' => 'array|min:3',
        'images.*' => 'image ',
    
    ]);
    foreach ($request->images as $image) {
        $image = request('image')->store('uploads', 'public');
        $image = Image::make(public_path("storage/{$imagepath}"))->fit(1200, 1200);
        $image->save();
    }
    post::create([
        'user_id' => auth()->user()->id,
        'Caption' => $data['PostCaption'],
        'image' => $image,
    ]);

    return redirect('/dashboard/' . auth()->user()->id);
}

I have done the changes but im geting this error : foreach() argument must be of type array|object, null given

MohamedTammam's avatar

@usman9023 Your input should look like

<input type="file" , class=" form-control-file ml-3 mt-2" id="image" name=" images[]" multiple>

Your controller method should look like

 $data = request()->validate([

        'PostCaption' => 'required',
        'images' => 'array|min:3',
        'images.*' => 'image ',
    
    ]);

	if($request->images) { // Or add "required" validation
		    foreach ($request->images as $image) {
        		$path = $image->store('uploads', 'public');
        		$image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200);
        		$image->save();
	    	}
	}

// etc ...

But in your Post you can't store image => $image because now they are multiple ones. So maybe you should make it one-to-many relationship.

usman9023's avatar

@MohamedTammam i have done the changes you have told me in my POST CONTROLLER

public function store(Request $request) {

    $data = request()->validate([

        'PostCaption' => 'required',
        'images' => 'array|min:3',
        'images.*' => 'image ',
    ]);
    if ($request->images) {
        foreach ($request->images as $image) {
            $path = $image->store('uploads', 'public');
            $image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200);
            $image->save();
        }
    }
    post::create([
        'user_id' => auth()->user()->id,
        'Caption' => $data['PostCaption'],
        'image' => $path,
    ]);

    return redirect('/dashboard/' . auth()->user()->id);
}

BUT NOW IM GETIING THIS ERROR : Undefined variable $path

Sinnbeck's avatar

@usman9023 You only want to store the path to the last image ? I suggest creating another table to store all image paths. A one to many relationship

MohamedTammam's avatar

@usman9023 Why are you passing the $path to the Post::create method. I mentioned that in my last reply

But in your Post you can't store image => $image because now they are multiple ones. So maybe you should make it one-to-many relationship.

AungHtetPaing__'s avatar

@usman9023 Currently your post table has image column. So you can have one post with one image path. You can't save multiple image path to one post. So If you single post can have multiple image, do one to many relationship(one post have many image path).

// image table (3 columns for example)
id, post_id, path

// post model
public function images() {
	return $this->hasMany(Image::class);
}

// controller
$post = post::create([
        'user_id' => auth()->user()->id,
        'Caption' => $data['PostCaption'],
    ]);

if ($request->images){
	foreach($request->images as $image) {
		$path = $image->store('uploads', 'public');
            $image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200);
            $image->save();
		Image::create([
			'post_id' => $post->id,
			'path' => $path
		]);
	}
}
1 like
usman9023's avatar

@AungHtetPaing__ @sinnbeck As Per your Instructions I have created a new table as follows create_image_table :

public function up() {

    Schema::create('images', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('post_id');
        $table->string('path');
        $table->timestamps();
    });
}

Post Model :

public function images()

{

  return $this->hasMany(Image::class);
}

Post Controller :

public function store(Request $request) {

    $data = request()->validate([

        'PostCaption' => 'required',
        'image.*' => 'required|image ',

    ]);
    $post = post::create([
        'user_id' => auth()->user()->id,
        'Caption' => $data['PostCaption'],
    ]);

    if ($request->images) {
        foreach ($request->images as $image) {
            $path = $image->store('uploads', 'public');
            $image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200);
            $image->save();
            Image::create([
                'post_id' => $post->id,
                'path' => $path
            ]);
        }
    }
    return redirect('/dashboard/' . auth()->user()->id);
}

create.blade.php :

                    <x-input-label for="image" :value="__('Image')" />

                    <input type="file" , class=" form-control-file ml-3 mt-2" id="image" name=" images[]" multiple>
                    <x-input-error :messages="$errors->get('image')" class="mt-2" />
                </div>

and also here is my posts table : public function up() {

    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->string('Caption');
        $table->string('image');
        $table->unsignedBigInteger('view_count')->default(0);
        $table->timestamps();
        $table->index('user_id');
    });
}
                

Now im getting this error :SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value I alredy put image field in post model in protected fillable . Dont know what im doing wrong ?

usman9023's avatar

@Sinnbeck if I remove my image column from my posts table then im getting this error :

GD Library extension not available with this PHP installation.

usman9023's avatar

@Sinnbeck if i remove my image column from my posts table then im getting this error :

"GD Library extension not available with this PHP installation".

Actually Im using Intervention image library. What should i do now to upload multiple images?

usman9023's avatar

@Sinnbeck i did what you told me and now im getting this error :

Call to undefined method Intervention\Image\ImageManager::create()

Sinnbeck's avatar

@usman9023 seems you are trying to use intervention as the image model. Show the full controller

usman9023's avatar

@Sinnbeck

class Image extends Facade {

protected static function getFacadeAccessor()
{
    return 'image';
}

}

Sinnbeck's avatar

@usman9023 Thats not a controller ? Ill just try explaining the problem to you

 $image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200); // Image refers to intervention

Image::create([ //You assume Image referes to \App\Models\Image, but it refers to intervention again

Try this

\App\Models\ Image::create([
                'post_id' => $post->id,
                'path' => $path
            ]);
usman9023's avatar

@Sinnbeck i dont have any image model i make following relation in posts model :

public function images() {

    return $this->hasMany(Image::class);
}
Sinnbeck's avatar

@usman9023 Then what are you refering to here?

public function images()
{
  return $this->hasMany(Image::class);
}

Hint: You need an image model

Sinnbeck's avatar

@usman9023 Yeah that would never work. You need an eloquent model. Just because a class has the correct name, does not mean you can just use it

1 like
usman9023's avatar

@Sinnbeck Thanks man i appreciate your help .. I will learn more about laravel then i will try this functionality thanks again man appreciate your help. During this problem i learn a lot from you

Please or to participate in this conversation.