To begin, implement the following:
class Customer extends Model
{
public function orders()
{
return $this->hasMany(Order::class);
}
}
class Order extends Model
{
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
Within the controller method responsible for displaying the Customer name selection view, include the following code:
class CustomerController extends Controller
{
public function show()
{
return view('some.view', ['customers' => Customer::get(['id', 'name']);
}
}
Utilize the $customers
variable to populate the select options on the view. Upon user interaction, send a request to the backend/server via ajax if using a JavaScript framework or through a plain POST request otherwise. Ensure to include the selected user's id
.
Handle the incoming request within the controller as demonstrated below:
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]);
}
}
Finally, make use of the $customer
and $orders
variables to render the view accordingly.