prodix's avatar

Backpack laravel issue

Hello, I have a products model where i want to count the total orders for each product without duplicating the orders. I also have an order_products table that has order_id and product_id.

The backpack crud needs to be albe to order by the filed where the count is displayed

The problem is that the orderBy count dosen't work properly, the count is corect but when you orderBy the list is not in perfect ascending or descendint order.

[ 'name' => 'order_numbers', 'label' => 'Orders', 'type' => 'closure', 'orderable' => true, 'function' => function ($entry) { return count($entry->orders); }, 'orderLogic' => function ($query, $column, $columnDirection) { return $query->withCount( [ 'orders', 'orders as orders_count' => function ($query) { $range = json_decode($this->crud->request->from_to); if ($range) { $query->whereBetween('order_products.created_at', [$range->from, $range->to]); } } ] )->orderBy('orders_count', $columnDirection); }, ],

0 likes
0 replies

Please or to participate in this conversation.