How to create laravel filter without button submit

I want to create some filter in dashboard admin panel, this filter using select dropdown, with option today, last 7 days, and last 30 days, and I won’t use button submit for get the data. I use this condition

    if (request()->ajax()) {
            if (!empty($request->filter)) {
                if ($request->filter == 1) {
                    $today = array(
                        $purchase_order = PurchaseOrder::whereDate('date_of_purchase', '=', Carbon::now())->get()->count(),
                        $sales_order = SalesOrder::whereDate('date_of_sales', '=', Carbon::now())->get()->count(),
                        $revenue = SalesOrder::whereDate('date_of_sales', '=', Carbon::now())->get()->sum('grand_total'),
                        $product_sales = ProductSalesOrder::leftJoin('product', 'product.product_id', '=', 'product_sales_order.product_id')
                                        ->leftJoin('sales_order', 'sales_order.sales_order_id', '=', 'product_sales_order.sales_order_id')
                                        ->whereDate('sales_order.date_of_sales', '=', Carbon::now())
                                        ->selectRaw('harga_beli * qty_order AS total_modal')->get()->sum('total_modal'),
                    );
                    echo json_encode($today);
                } elseif($request->filter == 2){
                    $a_week = array(
                        $purchase_order = PurchaseOrder::whereDate('date_of_purchase', '>=', Carbon::now()->subDays(7))->get()->count(),
                        $sales_order = SalesOrder::whereDate('date_of_sales', '>=', Carbon::now()->subDays(7))->get()->count(),
                        $revenue = SalesOrder::whereDate('date_of_sales', '>=', Carbon::now()->subDays(7))->get()->sum('grand_total'),
                        $product_sales = ProductSalesOrder::leftJoin('product', 'product.product_id', '=', 'product_sales_order.product_id')
                                        ->leftJoin('sales_order', 'sales_order.sales_order_id', '=', 'product_sales_order.sales_order_id')
                                        ->whereDate('sales_order.date_of_sales', '>=', Carbon::now()->subDays(7))
                                        ->selectRaw('harga_beli * qty_order AS total_modal')->get()->sum('total_modal'),
                    );
                    echo json_encode($a_week);
                }
                elseif ($request->filter == 3) {
                    $a_month = array(
                        $purchase_order = PurchaseOrder::whereDate('date_of_purchase', '>=', Carbon::now()->subDays(30))->get()->count(),
                        $sales_order = SalesOrder::whereDate('date_of_sales', '>=', Carbon::now()->subDays(30))->get()->count(),
                        $revenue = SalesOrder::whereDate('date_of_sales', '>=', Carbon::now()->subDays(30))->get()->sum('grand_total'),
                        $product_sales = ProductSalesOrder::leftJoin('product', 'product.product_id', '=', 'product_sales_order.product_id')
                                        ->leftJoin('sales_order', 'sales_order.sales_order_id', '=', 'product_sales_order.sales_order_id')
                                        ->whereDate('sales_order.date_of_sales', '>=', Carbon::now()->subDays(30))
                                        ->selectRaw('harga_beli * qty_order AS total_modal')->get()->sum('total_modal'),
                    );
                }

            }
        }

what shall I do in laravel blade?, it will be using ajax for getting the data?, or something

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

<select id="mySelect" onchange="filterData()">
   <option value="1">Today</option>
   <option value="2">Week</option>
   <option value="3">Month</option>
</select>

<script>
  function filterData() {
  let filter = document.getElementById("mySelect").value;

  // Here you need to ajax call the method by post request with filter value

  }
</script>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x