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

farhankhan786's avatar

Update function in laravel with image using api

When i use update function in laravel with image using api then PUT method is not updated the validation, why? when i using the post method for update function postman and routes its work? its my route Route::post('/update-product/{id}', [ProductController::class, 'update']); and its my function public function update(Request $request, $id) { $product = Product::findOrFail($id);

$request->validate([
    'name' => 'required|string|max:255',
    'image' => 'image|mimes:jpeg,png,jpg,gif|max:2048', 
    'price' => 'required|numeric|min:0',
    'quantity' => 'required|numeric|min:0',
    'status' => 'boolean',
]);

$product->name = $request->input('name');
$product->price = $request->input('price');
$product->quantity = $request->input('quantity');
$product->status = $request->input('status');
$product->save();

if ($request->hasFile('image')) {
    $imagePath = "storage/" . $request->file('image')->store("Product_image", "public");
    $formData['image'] = $imagePath;
}

return response()->json(['message' => 'Product updated successfully', 'product' => $product], 200);

}

0 likes
4 replies
vincent15000's avatar

First of all you should check if you have an image attribute.

if ($request->hasFile('image')) {
	dd('There is an image attribute in the request.');

    $imagePath = "storage/" . $request->file('image')->store("Product_image", "public");
    $formData['image'] = $imagePath;
}

If the message doesn't display on the screen, that means that you don't have any image attribute in the request.

Have you added an enctype attribute inside the form tag ?

1 like
farhankhan786's avatar

@vincent15000 I used postman for the api to check it the functionality but when i return response("image" ) in my if condition here is not coming because the validation can stop them, when i used the return response(request->all()) its give me empty array in postman->body->form-data.

1 like
vincent15000's avatar

@farhankhan786 I don't seen any if condition or $request->all() in your code. Please give an example of what you have tried.

1 like
farhankhan786's avatar

@vincent15000 $product = Product::findOrFail($id); return redirect($request->all()); $formData = $request->validate([ 'name' => 'required|string|max:255|unique:products,name,' . $id, 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 'price' => 'required|numeric|min:0', 'quantity' => 'required|numeric|min:0', 'status' => 'boolean', ]);

// Get the validated data
$formData = $request->all();

if ($request->hasFile('image')) {
    return response('There is an image attribute in the request.');
    $originalName = $request->file('image')->getClientOriginalName();
    $imageName = time() . '_' . $originalName;

    $imagePath = "storage/" . $request->file('image')->storeas("Product_image", "public");
    $formData['image'] = $imagePath;
}

$product->update($formData);

Please or to participate in this conversation.