Knowledge Base

Empty filter

Laravel Spatie User can see only his records

1.
public function index(Request $request)
{
$search = strtoupper($request->input('search', session('search')));
$exactMatch = $request->has('exact_match') ? true : session('exact_match', false);

session([
'search' => $search,
'exact_match' => $exactMatch,
]);

$query = KnowledgeBase::query()->where('status', 'published')->orderBy('id', 'desc');

// Apply role-based filtering
if (auth()->user()->hasRole('gost')) {
$query->where('user_id', auth()->id());
}

if (!empty($search)) {
$query->where(function ($q) use ($search, $exactMatch) {
if ($exactMatch) {
$q->whereRaw("UPPER(title) = ?", [$search])
->orWhereRaw("UPPER(content) = ?", [$search]);
} else {
$q->whereRaw("UPPER(title) LIKE ?", ['%' . $search . '%'])
->orWhereRaw("UPPER(content) LIKE ?", ['%' . $search . '%']);
}
});
}

$entries = $query->paginate(20);

return view('knowledge_base.index', compact('entries', 'search'));
}
2.
In table role_has_permissions add role_id and permission_id

3.
php artisan permission:cache-reset
php artisan config:cache
php artisan route:cache

4.@hasrole('admin|user')
@can('create articles')
<a href="{{ route('knowledge_base.create') }}"
class="text-red bg-yellow-600 hover:bg-yellow-700 font-medium py-2 px-4 rounded inline-block"
style="background-color:rgb(225, 240, 10);">
{{ __('Create New Entry') }}
</a>
@endcan
@endhasrole

or
5.
@if(auth()->user()->can('create articles'))
<a href="{{ route('knowledge_base.create') }}"
class="text-red bg-yellow-600 hover:bg-yellow-700 font-medium py-2 px-4 rounded inline-block"
style="background-color:rgb(225, 240, 10);">
{{ __('Create New Entry') }}
</a>
@endif

PROBLEM WITH LOGOUT
1.
When Blade code looks correct, but the problem seems to be that Jetstream expects the logout route to use the POST method, while your Blade template is sending a GET request because of this line:

html
Copy
Edit
<x-dropdown-link href="{{ route('logout') }}" @click.prevent="$root.submit();">
{{ __('Log Out') }}
</x-dropdown-link>
html
3 lines, 123 characters

Copy code
This does not actually submit the form. The x-dropdown-link generates an anchor (<a>) tag, which defaults to GET.

✅ Solution:
You can fix this by making sure the form submits correctly. Replace this part:

html
Copy
Edit
<form method="POST" action="{{ route('logout') }}" x-data>
@csrf
<x-dropdown-link href="{{ route('logout') }}"
@click.prevent="$root.submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
html
7 lines, 233 characters

Copy code
With this simpler version:

html
Copy
Edit
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit" class="block w-full text-left px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100">
{{ __('Log Out') }}
</button>
</form>

2.
php artisan view:clear
php artisan cache:clear
php artisan route:clear
php artisan config:clear

Published
Back to Index