Tidak ada masalah ketika saya membuat data baru. semuanya baik-baik saja termasuk mengunggah gambar. Gambar saya dimasukkan ke direktori publik/gambar. Tetapi ketika saya mencoba mengedit atau memperbarui, saya mengalami masalah. gambar saya yang harus dimasukkan ke publik/gambar tidak berfungsi pada fungsi pembaruan.

Pengontrolnya ada di bawah

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'sequence' => 'required',
            'image' => 'required|image|max:2048',
            'link' => 'required',
            'status' => 'required',
        ]);

        $image = $request->file('image');

        $new_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $new_name);

        $form_data = array(
            'name'             =>  $request->name,
            'sequence'         =>  $request->sequence,
            'image'            =>  $new_name,
            'link'             =>  $request->link,
            'status'           =>  $request->status,
        );

        Banner::create($form_data);

        return redirect()->route('banner.index');
    }

public function update(Request $request, Banner $banner)
        {
            $image_name = $request->hidden_image;
            $image = $request->file('image');
            if($image != ''){
                $request->validate([
                    'name' => 'required',
                    'sequence' => 'required',
                    'image' => 'image|max:2048',
                    'link' => 'required',
                    'status' => 'required',
                ]);
                $image_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
                $image->move(public_path('images'), $image_name);

            }

            else{
                $request->validate([
                    'name' => 'required',
                    'sequence' => 'required',
                    'link' => 'required',
                    'status' => 'required',
                ]);
            }

            $form_data = array(
                'name'             =>  $request->name,
                'sequence'         =>  $request->sequence,
                'image'            =>  $image_name,
                'link'             =>  $request->link,
                'status'           =>  $request->status,
            );

            Banner::whereId($banner)->update($form_data);
            return redirect()->route('banner.index');

        }

Dan kode tampilan saya di bawah

<form action="{{ route('banner.update',$banner->id) }}" method="POST">
                            @csrf
                            @method('PUT')

                            <div class="row" style="margin-top: 10px">
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group">
                                        <strong>Name:</strong>
                                        <input type="text" name="name" value="{{ $banner->name }}" class="form-control" placeholder="Name">
                                    </div>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group">
                                        <strong>Sequence</strong>
                                        <input type="number" name="sequence" value="{{ $banner->sequence }}" class="form-control" placeholder="Sequence">
                                    </div>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                <strong>Image</strong>
                                  <div class="col-md-8">
                                  <img src="{{ URL::to('/') }}/images/{{ $banner->image }}" class="img-thumbnail" width="200" />
                                  </div>
                                  <div class="col-md-8">
                                    <input type="file" name="image" /> 
                                    <input type="text" name="hidden_image" value="{{ $banner->image }}" />
                                   </div> 
                                  </div>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group">
                                        <strong>Link</strong>
                                        <input type="text" name="link" value="{{ $banner->link }}" class="form-control" placeholder="Link">
                                    </div>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group">
                                        <strong>Status</strong>
                                        <input type="text" name="status" value="{{ $banner->status }}" class="form-control" placeholder="Status">
                                    </div>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                                <button type="submit" class="btn btn-primary">Submit</button>
                                </div>
                            </div>

                        </form>
0
Soni Silalahi 19 Maret 2020, 20:27

2 jawaban

Jawaban Terbaik

Formulir Anda tidak memiliki atribut enctype sehingga tidak ada file yang lewat bersama formulir. tambahkan atribut itu dan semuanya akan berfungsi.

<form action="{{ route('banner.update',$banner->id) }}" method="POST" enctype="multipart/form-data">
1
zahid hasan emon 19 Maret 2020, 17:43

Saat Anda ingin mengunggah gambar POST dengan formulir Anda, Anda harus menambahkan atribut enctype ke formulir Anda:

<form action="{{ route('banner.update', $banner->id) }}" method="POST" enctype="multipart/form-data">
</form>
0
Cheeddar 19 Maret 2020, 18:56