Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL action generator in Laravel 6.x

I get a really weird error after I have updated to Laravel 6.4. Have I missed something?

Doesn't work

{{ action('Admin\OfferController@post', ['id'=>$offer->offer_id,'post'=>0]) }}

Works

{{ action('Admin\OfferController@post', [$offer->offer_id, 0]) }}

Error

Missing required parameters for [Route: offers.edit]

like image 608
szebasztian Avatar asked Oct 16 '25 12:10

szebasztian


1 Answers

Laravel 6.3(ish, I think) made the action url generator a little more strict, if you're naming the param (i.e. 'id'=> 0) then you have to make sure that name matches exactly the name of the param in the route definition.

You can find the name of the param required by using php artisan route:list and looking for the route you're trying to post to. In this case I suspect it should be 'offer' => $offer->offer_id instead of 'id' => $offer->offer_id but that's only if you generated the route using the route::resource method.

For example. in an app I'm currently working on the I have the following

Route::resource('/articles', 'ArticlesController');

Which generates the following route for updating an article

PUT /articles/{article}

So to use the action URL generator I have to do the following

action('ArticlesController@update', ['article' => $article->id])

The reason your second example works is because, in the absence of you telling it what that first variable is called, the URL generator assumes that the first param satisfies the param needed to generate the route.

like image 80
Alec Joy Avatar answered Oct 18 '25 09:10

Alec Joy