Gambar tidak tampil di folder public/store/uploads/buku dilaravel 10

Izin bertanya diaplikasi saya sudah berhasil untuk uploads gambar di database dan sudah masuk di folder uploads/buku yang sudah saya buat tetapi pada saat saya menampilkan ditampilan aplikasi tidak bisa muncul.

Kode Route
Route::group(['middleware' => 'auth'], function () {
    Route::get('/buku', [BukuController::class, 'index']);
    Route::get('/buku/store', [BukuController::class, 'create']);
    Route::post('/buku/store', [BukuController::class, 'store']);
    Route::get('/buku/{edit}/edit', [BukuController::class, 'edit']);
    Route::put('/buku/{id}', [BukuController::class, 'update']);
    Route::get('/buku/{id}', [BukuController::class, 'destroy']);
});

Kode Controller
<?php

namespace App\Http\Controllers;

use App\Models\Buku;

use Illuminate\Http\Request;

class BukuController extends Controller
{
    public function index()
    {
        $buku = Buku::get();
        // $buku = \App\Models\Buku::all();
        // dd($kategoris->all());
        return view("buku.index", compact("buku"));
    }

    public function create()
    {
        return view('buku.store');
    }

    public function store(Request $request)
    {
        $request->validate([
            'sampul' => 'nullable|mimes:png,jpg,jpeg,webp',
            'id_kategori' => 'required|max:255|string',
            'judul' => 'required|max:255|string',
            'penerbit' => 'required|max:255|string',
            'pengarang' => 'required|max:255|string',
            'tahun_terbit' => 'required|max:255|date',
            'jumlah' => 'required|max:255|integer',
            'is_active' => 'sometimes'
        ]);
        if ($request->has('sampul')) {

            $file = $request->file('sampul');
            $extension = $file->getClientOriginalExtension();

            $filename = time() . '.' . $extension;

            $path = 'uploads/buku';
            $file->move($path, $filename);
        }

        Buku::create([
            'sampul' => $path . $filename,
            'id_kategori' => $request->id_kategori,
            'judul' => $request->judul,
            'penerbit' => $request->penerbit,
            'pengarang' => $request->pengarang,
            'tahun_terbit' => $request->tahun_terbit,
            'jumlah' => $request->jumlah,
            'is_active' => $request->is_active == true ? 1 : 0,

        ]);
        return redirect("buku")->with('sukses', 'Data Berhasil disimpan');

    }

    public function edit($id)
    {
        $buku = \App\Models\buku::find($id);
        return view("buku.edit", compact("buku"));
    }
    public function update(Request $request, $id)
    {
        //kolom yang digunakan sesuai dengan nama tabel
        $buku = \App\Models\Buku::find($id);
        $buku->id_kategori = $request->id_kategori;
        $buku->judul = $request->judul;
        $buku->penerbit = $request->penerbit;
        $buku->pengarang = $request->pengarang;
        $buku->tahun_terbit = $request->tahun_terbit;
        $buku->jumlah = $request->jumlah;
        $buku->update();

        return redirect("buku")->with('sukses', 'Data Berhasil diupdate');
    }
    public function destroy($id)
    {
        $buku = \App\Models\Buku::find($id);
        $buku->delete();
        return redirect("buku")->with('sukses', 'Data Berhasil dihapus');
    }

}


kode Index / tampilan @extends('layout.master')

@section('title', 'buku')

@section('content') <div class="main-content"> <section class="section"> <div class="section-header"> <h1>Buku</h1> </div>

  &lt;div class=&quot;section-body&quot;&gt;
    &lt;div class=&quot;card&quot;&gt;
      &lt;div class=&quot;card-header&quot;&gt;
        &lt;h4&gt;Buku&lt;/h4&gt;


        &lt;div class=&quot;card-header-form&quot;&gt;
          &lt;button class=&quot;btn btn-sm btn-success&quot; type=&quot;button&quot; data-target=&quot;#modal-tambah&quot; data-toggle=&quot;modal&quot;&gt;Tambah
            Data&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;

      &lt;div class=&quot;card-body&quot;&gt;
        &lt;table class=&quot;table table-stripped&quot; id=&quot;table&quot;&gt;
          &lt;thead&gt;
            &lt;tr&gt;
              &lt;th style=&quot;width: 10%&quot;&gt;No&lt;/th&gt;
              &lt;th&gt;Sampul&lt;/th&gt;
              &lt;th&gt;Kode&lt;/th&gt;
              &lt;th&gt;Judul&lt;/th&gt;
              &lt;th&gt;Penerbit&lt;/th&gt;
              &lt;th&gt;Pengarang&lt;/th&gt;
              &lt;th&gt;Tahun Terbit&lt;/th&gt;
              &lt;th&gt;Jumlah&lt;/th&gt;
              {{-- &lt;th&gt;Deskripsi&lt;/th&gt; --}}
              &lt;th style=&quot;width: 15%&quot;&gt;Aksi&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;

          &lt;tbody&gt;
            @foreach ($buku as $item)
              &lt;tr&gt;
                &lt;td&gt;{{ $loop-&gt;iteration }}&lt;/td&gt;
                {{-- &lt;td&gt;
                  &lt;img src=&quot;/img/{{ $item-&gt;b_indonesia }}&quot; alt=&quot;{{ $item-&gt;sampul }}&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
                &lt;/td&gt; --}}
                {{-- &lt;td&gt;
                  &lt;img src=&quot;{{ asset($item-&gt;sampul) }}&quot; style=&quot;width: 70px; height:70px&quot; alt=&quot;Img&quot; /&gt;
                &lt;/td&gt; --}}

                &lt;td&gt;
                  &lt;img src=&quot;{{ asset(&#039;uploads/buku/&#039; . $item-&gt;sampul) }}&quot; style=&quot;width: 70px; height:70px&quot;
                    alt=&quot;Img&quot;&gt;
                &lt;/td&gt;

                &lt;td&gt;{{ $item-&gt;id_kategori }}&lt;/td&gt;
                &lt;td&gt;{{ $item-&gt;judul }}&lt;/td&gt;
                &lt;td&gt;{{ $item-&gt;penerbit }}&lt;/td&gt;
                &lt;td&gt;{{ $item-&gt;pengarang }}&lt;/td&gt;
                &lt;td&gt;{{ $item-&gt;tahun_terbit }}&lt;/td&gt;
                &lt;td&gt;{{ $item-&gt;jumlah }}&lt;/td&gt;
                &lt;td&gt;
                  &lt;form action=&quot;/buku/{{ $item-&gt;id }}&quot; id=&quot;delete-form{{ $item-&gt;id }}&quot;&gt;
                    @method(&#039;delete&#039;)
                    &lt;a href=&quot;/buku/{{ $item-&gt;id }}/edit&quot; class=&quot;btn btn-sm btn-warning&quot;&gt;&lt;i
                        class=&quot;fa fa-edit&quot;&gt;&lt;/i&gt;
                    &lt;/a&gt;
                    &lt;button class=&quot;btn btn-sm btn-danger&quot;
                      onclick=&quot;confirmDelete(&#039;delete-form{{ $item-&gt;id }}&#039;)&quot;&gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt;
                    &lt;/button&gt;

                  &lt;/form&gt;

                &lt;/td&gt;
              &lt;/tr&gt;
            @endforeach

          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

</div> @include('buku.form') @endsection

@push('script') <script> function confirmDelete(formId) { event.preventDefault() swal({ title: "apakah anda yakin?", text: "katika anda menekan Ok maka data anda akan terhapus", icon: "warning", buttons: true, dangerMode: true, }) .then((willDelete) => { if (willDelete) { document.getElementById(formId).submit() } }); }

$(document).ready(function() {
  $(&#039;#table&#039;).DataTable();
});

</script> @endpush

kode tambah data / form
<div class="modal fade" id="modal-tambah" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Tambah Data Buku</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="/buku/store" method="POST" enctype="multipart/form-data">
          @csrf

          {{-- <div class="form group">
            <label for="sampul">Sampul</label>
            <div class="controls">
              <input type="file" name="sampul" class="form-control" required>
            </div>
          </div> --}}

          <div class="form group">
            <label for="sampul">Sampul</label>
            <input type="file" name="sampul" class="form-control" required>
          </div>


          <div class="form group">
            <label for="id_kategori">Kode</label>
            <input type="text" name="id_kategori" id="id_kategori" class="form-control">
          </div>

          <div class="form group">
            <label for="judul">judul</label>
            <input type="text" name="judul" id="judul" class="form-control">
          </div>

          <div class="form group">
            <label for="penerbit">Penerbit</label>
            <input type="text" name="penerbit" id="penerbit" class="form-control">
          </div>

          <div class="form group">
            <label for="pengarang">Pengarang</label>
            <input type="text" name="pengarang" id="pengarang" class="form-control">
          </div>

          <div class="form group">
            <label for="tahun_terbit">Tahun Terbit</label>
            <input type="text" name="tahun_terbit" id="tahun_terbit" class="form-control">
          </div>

          <div class="form group">
            <label for="jumlah">Jumlah</label>
            <input type="text" name="jumlah" id="jumlah" class="form-control">
          </div>

          <button class="btn btn-sm btn-success" type="submit">Simpan</button>
        </form>
      </div>
    </div>
  </div>
</div>

avatar anonim_google_7130
@anonim_google_7130

1 Kontribusi 0 Poin

Diperbarui 1 bulan yang lalu

Tanggapan

hasil dd requestnya gimana gan?

1 Jawaban:

Buat kek gini aja mas. soalnya mas nyimpan data sampul ke database sekalian sama path-nya kan.

<img src="{{ asset($item->sampul) }}" />

oh iya kalo bisa bagian ini coba di ubah dikit jadi kek gini :

Buku::create([
       'sampul' => $path . '/' . $filename,
       'id_kategori' => $request->id_kategori,
       'judul' => $request->judul,
       'penerbit' => $request->penerbit,
       'pengarang' => $request->pengarang,
       'tahun_terbit' => $request->tahun_terbit,
       'jumlah' => $request->jumlah,
       'is_active' => $request->is_active == true ? 1 : 0,
]);
avatar wayosu
@wayosu

2 Kontribusi 0 Poin

Dipost 1 bulan yang lalu

Login untuk ikut Jawaban