TyForHelpDude
TyForHelpDude

Reputation: 5002

laravel pass parameter by href as variable instead of Request object

I have a link pass an id to a controller. The first scenario with $id paramter in contrller function it throws Missing argument 1 for App\Http\Controllers\ProductController::edit() error. when I change it to request it works what I expected, but I dont understand why laravel cant map id in query string to varaible $id in controller function??

Controller

public function edit($id)//this throws missing argument exception, if I change $id to Request $request it works..
{
    return view('frontend.product.create');
}

Blade

<a data href="{{ route('product.edit',['id'=>$product->id]) }}"><i class="fa fa-edit"></i> </a>

Routes

Route::get('product/edit', ['uses' => 'ProductController@edit', 'as'=>'product.edit']);

EDITED here are the changes I make and the result: Routes

  Route::get('product/edit/{id}', ['uses' => 'ProductController@edit', 'as'=>'product.edit']);

result:

NotFoundHttpException in compiled.php line 8277:
in compiled.php line 8277
at RouteCollection->match(object(Request)) in compiled.php line 7511
at Router->findRoute(object(Request)) in compiled.php line 7476
at Router->dispatchToRoute(object(Request)) in compiled.php line 7468
at Router->dispatch(object(Request)) in compiled.php line 2307
...

as you see it expects Request object but I want to send it just a varible..

Upvotes: 0

Views: 1924

Answers (2)

Laravel User
Laravel User

Reputation: 1129

You need to replace {id} in route with the $product->id value when you are making HTTP GET request.

Example :-

Suppose if your product id is 334455, then the url when making request will be https://www.yoursitename.com/product/edit/334455

Try this and it will work.

Upvotes: 1

Dov Benyomin Sohacheski
Dov Benyomin Sohacheski

Reputation: 7732

You need to add the parameter to your routes:

Route::get(
    'product/edit/{id}', // <-- Add this
    ['uses' => 'ProductController@edit', 'as'=>'product.edit']
);

Upvotes: 1

Related Questions