search feature on pulic page

This commit is contained in:
Htet Phone Aung 2023-09-08 00:32:45 +06:30
parent c012735176
commit 0ef892df4d
6 changed files with 33 additions and 11 deletions

View File

@ -17,7 +17,13 @@ class MangaController extends Controller
*/
public function index()
{
$mangas = Manga::latest('id')->paginate(10)->withQueryString();
$mangas = Manga::when(auth()->user()->role != 'admin', function($q) {
$user_id = auth()->id();
$q->where('author_id', $user_id);
})
->latest('id')
->paginate(10)
->withQueryString();
return view('manga.index', compact('mangas'));
}

View File

@ -12,10 +12,15 @@ class PageController extends Controller
public function index()
{
// Storage::disk('local')->put('example.txt', 'Contents');
$mangas = Manga::latest('id')->paginate(8)->withQueryString();
return view('index', [
'mangas' => $mangas
]);
$mangas = Manga::when(request()->has('search'), function($q) {
$keyword = request()->search;
$q->where('title', 'like', '%'.$keyword.'%');
})
->latest('id')
->paginate(8)
->withQueryString();
return view('index', ['mangas' => $mangas]);
}
public function manga($slug)

View File

@ -31,5 +31,6 @@ class AuthServiceProvider extends ServiceProvider
{
//gates here
Gate::define('admin-only', fn(User $user) => $user->role === 'admin');
Gate::define('dashboard', fn(User $user) => $user->role === 'admin' || $user->role === 'author');
}
}

View File

@ -3,12 +3,22 @@
@section('content')
<div class="row mt-5">
@if (request()->search)
<div class="d-flex align-items-center mb-3">
<h4 class="fw-semibold">#Search result by <span class="text-primary">{{ request()->search }}</span> </h4>
<a title="clear search" href="{{ route('page.index') }}" class="btn btn-danger ms-4 p-0 px-1 border-1 border-dark"> <i class="bi bi-x-lg"></i> </a>
</div>
@endif
<div class="col-12 col-md-9">
<div class="row flex-wrap">
@forelse ($mangas as $manga)
<x-outter-manga-frame :manga="$manga" />
@empty
<p class="text-center text-danger">No Manga Yet</p>
@if (request()->search)
<p class="text-center text-danger">Found nothing!!</p>
@else
<p class="text-center text-danger">No Manga Yet</p>
@endif
@endforelse
</div>
@ -23,7 +33,6 @@
</div>
</div>
</div>
@endsection
@section('footer')

View File

@ -17,9 +17,10 @@
</li>
</ul>
<form class="d-flex" role="search">
<!-- search box -->
<form class="d-flex" role="search" method="GET" action="{{ route('page.index') }}">
<div class="input-group">
<input class="form-control" type="search" placeholder="Search" aria-label="Search">
<input name="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-dark" type="submit"> <i class="bi bi-search"></i> </button>
</div>
</form>

View File

@ -13,7 +13,7 @@
@endguest
@auth
@can('admin-only')
@can('dashboard')
<a href="{{ route('home') }}" class="btn btn-sm btn-dark rounded-pill"> <i class="bi bi-speedometer"></i> Go To
Dashboard</a>
@endcan