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() 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')); return view('manga.index', compact('mangas'));
} }
@ -90,7 +96,7 @@ class MangaController extends Controller
public function destroy(Manga $manga) public function destroy(Manga $manga)
{ {
$this->authorize('delete', $manga); $this->authorize('delete', $manga);
$manga->delete(); $manga->delete();
return redirect()->back()->with(['message' => 'Manga has been deleted!']); return redirect()->back()->with(['message' => 'Manga has been deleted!']);
} }

View File

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

View File

@ -31,5 +31,6 @@ class AuthServiceProvider extends ServiceProvider
{ {
//gates here //gates here
Gate::define('admin-only', fn(User $user) => $user->role === 'admin'); 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') @section('content')
<div class="row mt-5"> <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="col-12 col-md-9">
<div class="row flex-wrap"> <div class="row flex-wrap">
@forelse ($mangas as $manga) @forelse ($mangas as $manga)
<x-outter-manga-frame :manga="$manga" /> <x-outter-manga-frame :manga="$manga" />
@empty @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 @endforelse
</div> </div>
@ -23,7 +33,6 @@
</div> </div>
</div> </div>
</div> </div>
@endsection @endsection
@section('footer') @section('footer')

View File

@ -17,9 +17,10 @@
</li> </li>
</ul> </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"> <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> <button class="btn btn-dark" type="submit"> <i class="bi bi-search"></i> </button>
</div> </div>
</form> </form>

View File

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