Saya mencoba mengedit produk di toko saya. Berdasarkan permintaan, $request->file('image'); Saya lampirkan kode sumber di bawah ini, saya benar-benar tidak tahu mengapa saya tidak menerima gambar dalam permintaan, karena saya pikir itu benar apa yang saya lakukan

Formulir saya:

<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>

Jika saya menghapus bagian dengan gambar, kode berfungsi sempurna Fungsi saya di pengontrol (ketik sumber daya)

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!");
    }
2
Anderson 12 Mei 2021, 17:52

1 menjawab

Jawaban Terbaik

Masalah inti di sini adalah value="{{ $product-> product_image }}" tidak valid. <input type="file"> tidak mendukung itu, karena gambar perlu diunggah langsung dari mesin Pengguna, dan kecuali jika file dipilih dan diunggah, $request->file('image') akan menjadi null.

Untuk menangani pembersih ini, gunakan beberapa logika kondisional di Controller:

Pertama, unggah gambar dan tetapkan referensi ke file:

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

Selanjutnya, atur $product->product_image berdasarkan nilai $profileImage:

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

Pernyataan ternary atau null-coalesce dapat digunakan, tetapi akan terlihat agak aneh:

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

Kedua kasus ini akan menyetel $product->product_image ke $profileImage, atau nilai $product->profile_image yang ada jika tidak ada yang disediakan (itu akan menjadi gambar yang ada atau null)

2
Tim Lewis 12 Mei 2021, 15:40