How to search code in laravel

Posted 1 week ago by irankhosravi

I want to search code for enter an admin.

web.php

Route::get('/document', '[email protected]')->name('document');

DocumentController.php

public function document()
{
    $keyword = request('code');
    $documents = Document::document($keyword)->latest()->get();
    return view('Home.content.documents', compact('documents'));
}

Document.php

public function scopeDocument($query, $keywords)
{
    $keywords = explode(' ',$keywords);
    foreach ($keywords as $keyword) {
        $query->where('code' , 'LIKE' , '%' . $keyword . '%');
    }
    return $query;
}

documents.blade,php

<form action="{{ route('document') }}" method="get">
    <div class="form-group">
        <label for="code">Code</label>
        <input type="text" class="form-control col-sm-4" id="code" name="code">
    </div>
    <button type="submit" class="btn btn-primary">Search</button>
</form>
<div class="text-center">
    @foreach($documents as $document)
        <h1 class="m-3">{{ $document->first_name }} {{ $document->last_name }}</h1>
        <img src="images/documents/{{ $document->image }}" class="img-fluid">
    @endforeach
</div>

Why this code is not working?

Please sign in or create an account to participate in this conversation.