Shahrukhlaravel's avatar

Image

I have an edit form which has an image field where a user can upload a new image if he wants to.

But if the user does not upload a new photo I don't want to validate the image field and just use the photo that's already in the database. And not update the image field at all.

public function update(UpdateProductRequest $request,$id)
{
    $getProduct = Product::find($id);

    if($file = $request->file('image'))
    {
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file->move('assets/img/products',$filename);
    }
    else{
        Product::find($id)->value('image');
    }

    $getProduct->update([
        'name' => $request->name,
        'price' => $request->price,
        'category' => $request->category,
        'brand' => $request->brand,
        'image' => $filename
    ]);
    return redirect()->back()->with('success','Product Updated Successfully');
}
0 likes
9 replies
jlrdw's avatar

With an if statement in controller checking if an image is part of the request.

If yes replace the image if no don't even mess with that part.

I deal with that the exact same way all the time.

Shahrukhlaravel's avatar

@jlrdw Then on validation for image update do we keep sometimes instead of required since validation runs first so that will always give an error for required field?

Ben Taylor's avatar

@Shahrukhlaravel if you put nullable as the first rule, it will only apply the other validation rules if an image has been uploaded

Shahrukhlaravel's avatar

Since I am new to it, can you tell me how in my code above or if you could show me a simple code, I'll be glad

jlrdw's avatar

@Shahrukhlaravel look for the example in the request chapter:

if ($request->hasFile('photo')) {
    //
}

It will guide you.

Shivamyadav's avatar

@Shahrukhlaravel do this and then try, And tell me one thing that are you using same controller method to edit and update your image?

public function update(UpdateProductRequest $request,$id)
{
    $getProduct = Product::find($id);
	$input = $request->all();

    if($file = $request->file('image'))
    {
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file->move('assets/img/products',$filename);
    }
    else{
        Product::find($id)->value('image');
    }

    $getProduct->update($input);
    return redirect()->back()->with('success', 'Product Updated Successfully');
}

Shahrukhlaravel's avatar

@Shivamyadav Edit and update function is in the same controller, secondly I tried your code but it didn't work out for me. Can you guide me a little more or if you could provide me code for this?

Shivamyadav's avatar

@Shahrukhlaravel try after removing the edit code

public function update(UpdateProductRequest $request,$id)
{
    //$getProduct = Product::find($id);
	$input = $request->all();

    if($file = $request->file('image'))
    {
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file->move('assets/img/products',$filename);
    }
    else{
        Product::find($id)->value('image');
    }

    $getProduct->update($input);
    return redirect()->back()->with('success', 'Product Updated Successfully');
}

Please or to participate in this conversation.