-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: how to apply default filters (from url parameters)? #2383
Comments
Hi @justinmoh , Question 1Accessing https://demo.backpackforlaravel.com/admin/monster?checkbox=true in the demo project goes to the Monsters CRUD, and activates the Simple filter for me. That's what I understand you needed, right? I haven't used this a lot, but that's how I do it. Worked for me. If it "redirects to itself and ditches the query string" I'm wondering what you're using in <?php
return [
/*
|--------------------------------------------------------------------------
| Backpack\CRUD preferences
|--------------------------------------------------------------------------
*/
// --------------------------
// Default operation settings
// --------------------------
'operations' => [
/*
* List Operation
*/
'list' => [
// Define the size/looks of the content div for all CRUDs
// To override per view use $this->crud->setListContentClass('class-string')
'contentClass' => 'col-md-12',
// enable the datatables-responsive plugin, which hides columns if they don't fit?
// if not, a horizontal scrollbar will be shown instead
'responsiveTable' => true,
// stores pagination and filters in localStorage for two hours
// whenever the user tries to see that page, backpack loads the previous pagination and filtration
'persistentTable' => true,
// How many items should be shown by default by the Datatable?
// This value can be overwritten on a specific CRUD by calling
// $this->crud->setDefaultPageLength(50);
'defaultPageLength' => 10,
// A 1D array of options which will be used for both the displayed option and the value, or
// A 2D array in which the first array is used to define the value options and the second array the displayed options
// If a 2D array is used, strings in the right hand array will be automatically run through trans()
'pageLengthMenu' => [[10, 25, 50, 100, -1], [10, 25, 50, 100, 'backpack::crud.all']],
// How important is it for the action buttons to be visible?
// - 0 - most important
// - 1 - as important as bulk buttons
// - 2-3 - more important than the rest of the columns
// - 4 - less important than most columns
'actionsColumnPriority' => 1,
],
// ... Question 2You don't have to use filters. For your use case, where the admins shouldn't even be able to see things that are note pending/approved/completed, you should probably modify the query in setup(), so they only get to see what they're supposed to. The query is exposed and modifiable in Backpack. In you switch ($admin_type) {
case 'A':
$this->crud->query = $this->crud->query->where('status', 'pending');
break;
case 'B':
$this->crud->query = $this->crud->query->where('status', 'approved');
break;
case 'C':
$this->crud->query = $this->crud->query->where('status', 'completed');
break;
} This would also solve your problem with the total number of items. You solution with multiple models is cool too. Arguably, it's even cleaner :-) And you can use the same filtered models everywhere inside the app to make sure they only get to see their stuff. Which is cool. But personally I prefer one model per database table. I would do it like I detailed for Question 2 above. Hope it helps. |
(closing the issue so it doesn't show up in our list of things to-do; feel free the continue the conversation or for others to pitch in with their opinion) |
Managed to get something similar working and any improvement is much appreciated.. i needed to make a column that link to the child model and and show only the linked items in the table therefore i wrote this in a trait
In the crud controller for the related (belongs to) model i used this to filter
` it worked for me |
Thank you, I really really need this. I will try to implement this, but if you don't mind, can you share the detail for me? How is the trait, how to implement the trait in the entityController? After trying searching more about this topic, I think this is the answer for me. https://backpackforlaravel.com/articles/tutorials/nested-resources-in-backpack-crud |
@haniramadhan-kkp just an heads up that you have a nested crud example on demo if you want some code example on how to implemente it: Laravel-Backpack/demo#568 Cheers |
Something that have been troubled me since the day I started using Backpack.
Question 1
How to apply filter from url parameters? Visiting
backpack_url('booking').'?only_pending=1
will just redirect to itself and ditch the querystring.In a CRUD controller, we have a list operation, and we can define multiple filters that make sense for this
Entity
. Then in an example scenario:Admin-A
wants to see only Pending booking list by defaultAdmin-B
wants to see only Approved booking list by defaultAdmin-C
wants to see only Completed booking list by defaultFrom the developer point of view, they're actually just a matter of applying filter on the underlaying entity CRUD controller list operation -- but I don't know how to implement this.
Question 2
The next issue closely related.
Backpack
doesn't expose the API to directly amend the base model query. All query must be added byCRUD::addClause()
, which bybackpack
's own understanding, it's afilter
.So if
Admin-A
is a user that only allowed to see Pending list and nothing else, and developer useCRUD::addClause('onlyPending')
onsetupListOperation()
, the user will indeed seeing something likeshowing x from 12,345 filtered result
.Then
Admin-A
will then ask the developer how he can see the 12,345 items. But in fact he only entitled to see thatx
items.This is arguably a "visual" issue, but I think it's more about framework architecture.
So what do I do?
Anyway, for the situation that I've been facing, I create a dedicated model for each List, and in each of them, I only utilise the list operation. E.g.
I'm asking if there's a better way from experts like you guys.
The text was updated successfully, but these errors were encountered: