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
class Customer extends Model
{
public function orders()
{
return $this->hasMany(Order::class);
}
}
class Order extends Model
{
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
In the controller method which returns the view to select Customer name
class CustomerController extends Controller
{
public function show()
{
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
class SomeController extends Controller
{
public function unfulfilledOrders(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"