Form with enctype return null file in request

Viewed 49

I tried to edit the products in my store. On request, missing $request->file('image'); I'm attaching the source code below, I really don't know why I don't receive the image in the request, because I think it's correct what I did

My form :

<form  method="POST" action="{{ route('products.update', $product->id)}}" enctype="multipart/form-data">
        @csrf
        @method('PATCH')
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput1">Product slug</label>
                    <input type="text" name="product_slug" value="{{$product->product_slug}}" class="form-control" id="exampleFormControlInput1" placeholder="Enter slug">
                  </div>
            </div>
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput111">Product title</label>
                    <input type="text" name="product_title"  value="{{$product->product_title}}" class="form-control" id="exampleFormControlInput111" placeholder="Enter slug">
                  </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput2">Product category</label>
                    <input type="text" name="product_category"  value="{{$product->product_category}}" class="form-control" id="exampleFormControlInput2" placeholder="name@example.com">
                  </div>
            </div>
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput3">Product brand</label>
                    <input type="text" name="product_brand"  value="{{$product->product_brand}}" class="form-control" id="exampleFormControlInput3" placeholder="name@example.com">
                  </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput22">Product display</label>
                    <input type="text" name="product_display"  value="{{$product->product_display}}" class="form-control" id="exampleFormControlInput22" placeholder="name@example.com">
                  </div>
            </div>
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput34">Product ram</label>
                    <input type="text" name="product_ram"  value="{{$product->product_ram}}" class="form-control" id="exampleFormControlInput34" placeholder="name@example.com">
                  </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput33">Product os</label>
                    <input type="text" name="product_os"  value="{{$product->product_os}}" class="form-control" id="exampleFormControlInput33" placeholder="name@example.com">
                  </div>
            </div>
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput333">Product camera</label>
                    <input type="text" name="product_camera"  value="{{$product->product_camera}}" class="form-control" id="exampleFormControlInput333" placeholder="name@example.com">
                  </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlInput8">Product price</label>
                    <input type="text" name="product_price"  value="{{$product->product_price}}" class="form-control" id="exampleFormControlInput8" placeholder="name@example.com">
                  </div>
            </div>
            <div class="col">
                <div class="form-group">
                    <label for="exampleFormControlFile1">Change product photo</label>
                    <input type="file" name="image" value="{{$product->product_image}}" class="form-control-file" id="exampleFormControlFile1">
                    <img src="/storage/img/tech/{{$product->product_image}}" style="width:300px" alt="product_image">
                  </div>
            </div>
        </div>


        <div class="form-group">
          <label for="short_description">Short description</label>
          <textarea class="form-control" name="about_product" id="short_description" rows="10">{{$product->about_product}}</textarea>
        </div>
        <div class="form-group">
            <label for="long_description">Long description</label>
            <textarea class="form-control" name="product_description"   id="long_description" rows="10">{{$product->product_description}}</textarea>
          </div>
          <input type="submit" value="Edit" class="btn btn-success" name="submit">
          <a href="/admin/products" class="btn btn-primary">Go back</a>
      </form>

If I remove the part with the image, the code is perfectly functional My function in controller(type resource)

public function update(Request $request, $id)
    {
        dd($request->all());
        $request->validate([
            'product_slug' => 'required|max:100',
            'product_title' => 'required|max:100',
            'product_category' => 'required|max:100',
            'product_brand' => 'required|max:100',
            'product_image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'product_display' => 'required',
            'product_camera' => 'required',
            'product_ram' => 'required',
            'product_os' => 'required',
            'product_price' => 'required|max:100',
            'about_product' => 'required',
            'product_description' => 'required'
        ]);

            $input = $request->all();

        if ($image = $request->file('image')) {
                $destinationPath = 'storage/img/tech/';
                $profileImage = $image->getClientOriginalName();
                $image->move($destinationPath, $profileImage);
                $input['image'] = $profileImage;
        } else {
            unset($input['image']);
        }
        $product = Product::find($id);
        $product->product_slug = $request->get('product_slug');
        $product->product_title = $request->get('product_title');
        $product->product_category = $request->get('product_category');
        $product->product_brand = $request->get('product_brand');
        $product->product_display = $request->get('product_display');
        $product->product_ram = $request->get('product_ram');
        $product->product_camera = $request->get('product_camera');
        $product->product_os = $request->get('product_os');
        $product->product_price = $request->get('product_price');
        $product->product_image = $profileImage;
        $product->about_product = $request->get('about_product');
        $product->product_description = $request->get('product_description');
        $product->update();


        return redirect('/admin/products')->with('success', "product updated!");
    }
1 Answers

The core issue here is that value="{{ $product-> product_image }}" is not valid. <input type="file"> doesn't support that, as the image needs to be directly uploaded from the User's machine, and unless a file is selected and uploaded, $request->file('image') will be null.

To handle this cleaner, use some conditional logic in the Controller:

First, upload the image and set a reference to the file:

$profileImage = null;
if ($image = $request->file('image')) {
  $destinationPath = 'storage/img/tech/';
  $profileImage = $image->getClientOriginalName();
  $image->move($destinationPath, $profileImage);
}

Next, set the $product->product_image based on the value of $profileImage:

$product = Product::find($id);
...
if ($profileImage)
  $product->product_image = $profileImage;
}

A ternary or null-coalesce statement can be used, but it'll look a bit weird:

$product->product_image = $profileImage ? $profileImage : $product->product_image;
// OR
$product->product_image = $profileImage ?? $product->product_image;

Both of these cases will set $product->product_image to $profileImage, or the existing value of $product->profile_image if nothing is provided (that will be an existing image or null)

Related