Search code examples
laravelgetroutes

Getting ID from url and using it in CRUD controller laravel


I need to add orders to existing customers. From my customers.show view I use the following to visit the order form.

href="{{action('OrderController@create', $customer['id'])}}

The link of the page looks like:

/orders/create?2f10f8a0-30b9-11e8-8711-dd477b141226

I've been using the following route::get to no avail to be able to use this ID in my resource controller for Orders

Route::get('orders/create/{id}',  'OrderController@create');

Basically need to be able to to something along the lines of below to be able to show customer details while displaying the create order form as well but getting stuck on best way to pass the id through for the current customer

public function create(){
      $customer = Customer::find($id)
      return view('orders.create', compact('customer'));
}

Solution

  • With Laravel, you just need to add the parameters you specified in the route as function parameters, like below:

    public function create($id){
          $customer = Customer::find($id)
          return view('orders.create', compact('customer'));
    }