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

soufianeb's avatar

Undefined array key "image"

Hello, I have a problem with my laravel project (I'm quite new to Laravel). So I'm building an e-commerce and everything was fine until i decided to add an image to my create new product. Now when I am submitting my form i have an error: Undefined array key "image".

So i added a string image in my DB table so that i can save the name of my image: ​Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('naam'); $table->text('beschrijving'); $table->integer('user_id'); $table->integer('prijs')->default(0); $table->string('image'); $table->timestamps(); }); ​ (Before wanting to add image, everything was working fine when creating new products)

Here is my function in my controller:

public function store(Request $request){
        $image = $request->file('product-image');
        $image -> move('images', $image->getClientOriginalName());
        $imageName = $image->getClientOriginalName();

        $validated = $request->validate([

        'name'=>'required|min:3',
        'beschrijving'=>'required|min:20',
        'prijs'=>'required|min:1',
        'image'=>$imageName
               
    ]);

    $product= new Product;
    $product->naam = $validated['name'];
    $product->beschrijving= $validated['beschrijving'];
    $product->prijs= $validated['prijs'];
    $product->image= $validated['image'];
    $product->user_id = Auth::user()->id;
    $product->save();

    return redirect()->route('index')->with('status','Product added');
    }

When I am running this on my localhost, it shows me that the problem is in line: $product->image= $validated['image'];

And if necessary, here is my blade.php:

<div class="row mb-3">
    <label class="col-md-4 col-form-label text-md-end">Product image</label>
           <div class="col-md-6">
                 <input type="file" class="form-control" name="product-image" value="">
                       @error('name')
                           <span class="invalid-feedback" role="alert">
                                 <strong>{{ $message }}</strong>
                           </span>
                       @enderror
            </div>
 </div>

Thanks

0 likes
2 replies
Sinnbeck's avatar

This isn't a valid validation rule $imageName

This is

'image'=> 'image'

And just do

$product->image= $imagename;

And move validation to before you move the image file

Snapey's avatar

validate FIRST, before moving the image

Please or to participate in this conversation.