How does one create a custom filtered list view? #1317
Replies: 2 comments
-
I'll help you create a custom filtered list view in Backpack that restricts attendees based on event ownership. This is a common requirement when implementing access control in admin panels. Filtering Data Based on User AccessThe best approach is to use Option 1: Using Query Parameters with Base ClausesIn your protected function setupListOperation()
{
// Get event_id from query parameter
$event_id = request()->query('event_id');
if ($event_id) {
// Check if current user is an owner of this event
$isOwner = \App\Models\EventOwner::where('user_id', backpack_user()->id)
->where('event_id', $event_id)
->exists();
if ($isOwner) {
// Only show attendees for this event
CRUD::addBaseClause('where', 'event_id', $event_id);
} else {
// User is not authorized to see this event's attendees
abort(403);
}
} else {
// If no event_id specified, only show attendees for events the user owns
CRUD::addBaseClause('whereIn', 'event_id', function($query) {
$query->select('event_id')
->from('event_owners')
->where('user_id', backpack_user()->id);
});
}
// Rest of your list setup...
} The issue with the edit links is likely because you're not properly handling the route. You need to override the public function edit($id)
{
// Get the attendee
$attendee = $this->crud->getEntry($id);
// Check if current user is an owner of this attendee's event
$isOwner = \App\Models\EventOwner::where('user_id', backpack_user()->id)
->where('event_id', $attendee->event_id)
->exists();
if (!$isOwner) {
abort(403);
}
return parent::edit($id);
} Option 2: Custom Routes ApproachFor the custom route approach ( Route::get('event/{event_id}/attendee', 'AttendeesCrudController@eventAttendees');
Route::post('event/{event_id}/attendee/search', 'AttendeesCrudController@search'); Then in your controller: public function eventAttendees($event_id)
{
// Check if current user is an owner of this event
$isOwner = \App\Models\EventOwner::where('user_id', backpack_user()->id)
->where('event_id', $event_id)
->exists();
if (!$isOwner) {
abort(403);
}
// Filter attendees by event_id
$this->crud->addClause('where', 'event_id', $event_id);
return $this->index();
} The error you're encountering is likely related to how Backpack handles the custom routes and AJAX requests for the DataTables. This approach requires more customization of the controller methods. Best PracticesBased on the knowledge sources, the most reliable approach is to:
For your specific case, I recommend the first approach with proper access control in the controller methods, as it's simpler and follows Backpack's patterns more closely. Remember that Let me know if you need more specific guidance on implementing any of these approaches! ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: List Operation|List Operation |
Beta Was this translation helpful? Give feedback.
-
Hello @borrows08 can you share Controller and Model to replicate in my side. Did you try the approach our bot suggest to you? Cheers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been trying to follow the documentation but I have not found an easy way to be able to filter a list view.
I have three tables:
The event_owners table dictate who's supposed to have access to the event. Only event owners should be able to view the list of attendees based on users.id and event_owners.user_id and events.id. In the list of attendees CRUD, it should list only attendees for a specific event that the current user has access to.
For that particular use, I tried implementing:
/admin/attendee?event_id=XX
I used
addBaseClause()
so that the filtered view would show only total number of entries that match the event ID. However, I'm encountering issues with Edit (and probably Delete too) because the Edit link becomes: /admin/attendee?event_id=XX/yy/edit. Ideally, we should have just the primary key: /admin/attendee/123/edit.I also tried implementing a custom route:
/admin/event/1/attendee
When I do this, I'm getting a bunch of different errors that look like this:

During one of my attempts, I also encountered this error which seems to suggest that there is an issue with the way I'm customizing the list view:
What is the best approach for this? Is it possible to customize Backpack so that it can hide data users are not supposed to have access to?
Beta Was this translation helpful? Give feedback.
All reactions