Skip to content Skip to sidebar Skip to footer

Select Specific Data Based On Select Option

I want to select a customer name in a select dropdown list and gets its orders numbers where order status=0 On this order it contain a list of vehicles example T1,T2,T3..... So aft

Solution 1:

So what you can do is

classCustomerextendsModel{
    publicfunctionorders()
    {
        return$this->hasMany(Order::class);
    }
}

classOrderextendsModel{
    publicfunctioncustomer()
    {
        return$this->belongsTo(Customer::class);
    }
}

In the controller method which returns the view to select Customer name

classCustomerControllerextendsController{
    publicfunctionshow()
    {
        return view('some.view', ['customers' => Customer::get(['id', 'name']);
    }
}

Use the $customers to populate select options in the view. Once user clicks/selects a user, send request to the backend/server via ajax if you are using a javascript framework or plain POST request in case you are not using javascript framework for frontend. Remember to pass the id of the user selected by the visitor(user).

Then tackle the request in controller

classSomeControllerextendsController{
    publicfunctionunfulfilledOrders(Request $request, $id)
    {
        $customer = Customer::findOrFail($id);        
        $orders = $customer->orders()->where('status', 0)->get();

        return view('orders.show', ['orders' => $orders, 'customer' => $customer]);
    }
}

Then use the $customer and $orders variables to populate the view.

Post a Comment for "Select Specific Data Based On Select Option"