Riza Setiawan
Riza Setiawan

Reputation: 45

can't show the result of search in laravel

i have little problem about use search feature in laravel, everything was working. but when i search some buku it not show at all.

this is my search function

public function search(Request $request){
    $bukudn = bukudn::when($request->keyword, function($query) use ($request) {
        $query = bukudn::where('namaBuku', 'LIKE',"%'($request->keyword)'%")
                                ->orWhere('penulis', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('namaPenerbit', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('tahunTerbit', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('kotaPenerbit', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('mataPelajaran', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('bahasa', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('tingkatPendidikan', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('jurusan', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('edisi', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('rakBuku', 'LIKE', "%'.($request->keyword)'%")
                                ->orWhere('jumlahBuku', 'LIKE', "%'.($request->keyword)'%");

    })->paginate();



    $bukudn->appends($request->only('keyword'));

    return view('daftarbukuDN.search', compact('bukudns'));
}

this is my search.blade.php

<form action="{{ url()->current() }}" >
        <div class="input-group">
        <br>
            <input type="text" class="form-control" name="keyword"
                placeholder="Search users"> <span class="input-group-btn">
                <div class="col-md-10">
                <button type="submit" class="btn btn-primary">
                Search
                </button>
                </div>
            </span>
        </div>
    </form>
    <div class="table-responsive">
    <table class="table table-bordered">
            <thead>
                </div><br>
                <tr>
                <th>Nama Buku</th>
                <th>Penulis</th>
                <th>Nama Penerbit</th>
                <th>Tahun Terbit</th>
                <th>Mata Pelajaran</th>
                <th>Bahasa</th>
                <th>Tingkat Pendidikan</th>
                <th>Edisi Buku</th>
                <th >Action</th>
            </tr>
            </thead>
            <tbody>
             @if(isset ($bukudns))
                @foreach($bukudns as $bukudn)
              <tr>
                <td>{{$bukudn->namaBuku}} </td>
                <td>{{$bukudn->penulis}} </td>
                <td>{{$bukudn->namaPenerbit}} </td>
                <td>{{$bukudn->tahunTerbit}} </td>
                <td>{{$bukudn->mataPelajaran}} </td>
                <td>{{$bukudn->bahasa}} </td>
                <td>{{$bukudn->tingkatPendidikan}} </td>
                <td>{{$bukudn->edisi}} </td>
                <td>
                    <a href="{{action('bukudnController@show', $bukudn['id'])}}" class="btn btn-info">Lihat</a>
                </td>
                <td><a href="{{action('bukudnController@edit', $bukudn['id'])}}" class="btn btn-warning">Ubah</a></td>
                <td>
                    <form action="{{action('bukudnController@destroy', $bukudn['id'])}}" method="post">
                    @csrf
                    <input name="_method" type="hidden" value="DELETE">
                    <button class="btn btn-danger" type="submit">Hapus</button>
                    </form>
                </td>
              </tr>
                  @endforeach
              @endif
              </tbody>
            </tbody>
            </table>
            </div>

the problem is my program don't show any result of search, maybe this screenshoot wil explain my problementer image description here

Upvotes: 0

Views: 273

Answers (2)

Erubiel
Erubiel

Reputation: 2972

If this is your actual code, you just got a typo on:

$bukudn->appends($request->only('keyword'));

return view('daftarbukuDN.search', compact('bukudns'));

either change compact to compact('bukudn') or the variable name to $bukudns

Upvotes: 1

Mtxz
Mtxz

Reputation: 3869

I'm not sure that the following line do what you want :

$query = bukudn::where('namaBuku', 'LIKE',"%'($request->keyword)'%");

Could you try with this

$query = bukudn::where('namaBuku', 'LIKE',"%".$request->keyword."%");

You may need to sanitize $request->keyword

Upvotes: 0

Related Questions