Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Extra List View to Crud Controller #627

Closed sharat-rhinofish closed 1 year ago

sharat-rhinofish commented 1 year ago

I have an "orders" entity, which has the list view is accessible with the /admin/order URL. I want to have an additional list view which only as a list of orders for approval and another list which has orders for production. I do not want to use Filters for these additional list views as they are accessible by users with different permissions and are primarily meant to pass orders through an approval and production process.

I see from https://backpackforlaravel.com/docs/6.x/crud-operation-list-entries#custom-query-1 that I can add a custom query to the List view. What is the easiest way to add these additional routes: /admin/order/approve - To list orders needing approval. /admin/order/production - To list orders needing production.

In both these routes, I just want to define the additional query which will add the conditions to list orders needing approval/orders for production. I want to leverage all of the rest of the list functionality, so that the columns, the filters, the buttons, etc, including the detailed information on the order remain the same.

With Regards, Sharat

promatik commented 1 year ago

Hi @sharat-rhinofish!

There is 2 options in my opinion, but you've already excluded the first one 😅

1) Use filters You can have different sidebar entries, pointing to different URLs, each one with different filters /admin/order/ /admin/order/?status=approved /admin/order/?status=production It's the easiest solution possible, very simple to implement also, not so easy to add permission validation.

2) Create another controller It seems to much, but it's not 👌 You have your OrderCrudController.php, with the default stuff. You can create another controller OrderApprovedCrudController.php that will extend the default one 👌 OrderApprovedCrudController.php will be almost an empty class, just with the things you want to "override". So you'll have to add the addClause somewhere, extend the base controller, and that's it;

class OrderApprovedCrudController extends OrderCrudController
{
    public function setup()
    {
        parent::setup();

        CRUD::addClause('where', 'status', OrderStatus::APPROVED);
    }
}

Don't forget to also set the routes, and the permissions validation 👌 Let me know if it solved your issue.

sharat-rhinofish commented 1 year ago

Hi @promatik ,

Thanks for the excellent suggestion. I decided to use the first approach itself with a little modification. I added a queue parameter to the left nav item as follows: backpack_url('order?queue=approve')

I then check if the queue parameter is set and have modified the setupListOperation() function as follows:

    protected function setupListOperation()
    {
        // Get "queue" param and addClause to filter list if needed.
        $queue = $_GET['queue'] ?? "";
        switch ($queue) {
            case self::QUEUE_APPROVE:
                // Order Items which are Pending Approval.
                CRUD::addClause('whereHas', 'orderItems', function ($query) {
                    $query->where('status', OrderItem::STATUS_PENDING_APPROVAL);
                });
                break;
            case self::QUEUE_PRODUCTION:
                // Order Items which are Ready, Awaiting Inventory or In Work.
                CRUD::addClause('whereHas', 'orderItems', function ($query) {
                    $query->whereIn('status', [OrderItem::STATUS_READY, OrderItem::STATUS_AWAITING_INVENTORY, OrderItem::STATUS_IN_WORK]);
                });
                break;
        }
...

Click "Reset" on the List view shows all orders, but that is fine, as I plan to over-ride the view.

The second solution you gave is also great and useful. Thank you for the excellent suggestions.