Extend a CrudController with another Crud... example: ReceivableCrudController extends InvoiceCrudController... InvoiceCrudController extends CrudController. In this case, a receivable is a type of invoice, so the crud is handled slightly differently.
I declared the filter types on the parent (InvoiceCrudController) and expected them to work on the child crud (ReceivableCrudController).
The filters did not work unless I declared them directly on the ReceivableCrudController; whichever one is the active one on the route.
Backpack 3.4; Laravel 5.6; PHP 7.2; Latest MariaDB
Hello,
I assume you defined a setup() function within both controller to connect to different models correct? If thag is the case it is because the new setup() where to define the filters overwrites the parent one. Just as any controller backpack or not what do.
Yup! And I've got parent::setup(); in the child crud.
The filters show up and "function" properly with the exception that they don't have any impact on the results below... unless the filters are defined on the crud related to the route you're viewing.
Basically they create a link like this:
https://app.dev/admin/receivable/search?status=active
then it reloads the datatable with the updated information. I'm curious if it is even trying to apply the filter or not. Overwrite the resources/views/vendor/backpack/inc/filters_navbar.blade.php with this (all it adds is a console.log line)
https://gist.github.com/AbbyJanke/96172fd13eacaf8ead26af5ba315e59c
Then check your browser console. It should have a URL similar to what i mentioned if not it, means that the filters are even triggering anything.
@xavinci I recommend you isolate everything you need to do in both setup() methods in its own method, then call them in both setup() methods. So you'd have:
class InvoiceCrudController extends CrudController {
public function setup() {
// setModel, setRoute, setEntityNameStrings
// addColumns
// addFields
$this->addInvoiceFilters();
}
public function addInvoiceFilters() {
// addFilters
}
}
class ReceivableCrudController extends InvoiceCrudController {
public function setup() {
// setModel, setRoute, setEntityNameStrings
// addColumns
// addFields
$this->addInvoiceFilters();
}
}
But please note that the filters you defined on the Invoice model will now apply to the Receivable model. It will NOT apply them to the parent model (Invoice). This would only work if Invoice and Receivable are very _very_ similar - if they have the same db columns. Otherwise they couldn't possibly work.
For example, if you're trying to filter by receivable.category_id, but there is no such thing, only receivable.invoice.category_id, you need to redefine your filters in ReceivableCrudController, using that relationship. You need different addClause statements in the addFilter() closure. Using whereHas and such.
Closing this issue as I don't think it's a bug.
Cheers!
Thanks Cristian and Abby!
Sorry I went MIA on this… I dug a little deeper and I decided to set these up as
their own extensions of crudcontroller vs. extending each other… much cleaner
this way too.
Appreciate the responses and help!
James
On Wed, Jul 11, 2018 3:32 AM, Cristian Tabacitu [email protected] wrote:
@xavinci I recommend you isolate everything you need to do in both setup()
methods in its own method, then call them in both setup() methods. So you'd
have:
class InvoiceCrudController extends CrudController {
public function setup() {
// setModel, setRoute, setEntityNameStrings
// addColumns
// addFields
$this->addInvoiceFilters();
}
public function addInvoiceFilters() {
// addFilters
}
}
class ReceivableCrudController extends InvoiceCrudController {
public function setup() {
// setModel, setRoute, setEntityNameStrings
// addColumns
// addFields
$this->addInvoiceFilters();
}
}
But please note that the filters you defined on the Invoice model will now
apply to the Receivable model. It will NOT apply them to the parent model
(Invoice). This would only work if Invoice and Receivable are very very similar
For example, if you're trying to filter by receivable.category_id, but there is
no such thing, only receivable.invoice.category_id, you need to redefine your
filters in ReceivableCrudController, using that relationship. You need different
addClause statements in the addFilter() closure. Using whereHas and such.
Closing this issue as I don't think it's a bug.
Cheers!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.