Luiz
Luiz

Reputation: 105

Create with arguments [Laravel 5.2]

I want to use a argument in a create action, but when I try to access the action:

Missing argument 1 for App\Http\Controllers\AdsDiagController::create()

Here is the create action:

public function create($id){
    $record = TestRecord::findOrFail($id);
    return view("adsdiag.create", ["record" => $record]);
}

Here is the link to the action:

<a href="{!! action('AdsDiagController@create', $record->id ) !!}">Create</a>

And the route:

Route::resource('adsdiag', 'AdsDiagController');

I'm newbie in laravel, and I'm really confused with routes. I appreciate any help.

Upvotes: 4

Views: 1760

Answers (2)

imrealashu
imrealashu

Reputation: 5099

To solve your problem you should use in your route.php

Route::get('adsdiag/{id}/',AdsDiagController@create);

Reason

When you call Route::resource('adsdiag', 'AdsDiagController') it generates these routes

Route::get('adsdiag','AdsDiagController@index');
Route::post('adsdiag','AdsDiagController@store');
Route::get('adsdiag/create','AdsDiagController@create'); // you can see that create method doesn't have any arguments here.
Route::get('adsdiag/show/{id}','AdsDiagController@show');
Route::post('adsdiag/update','AdsDiagController@update');
Route::get('adsdiag/edit/{id}','AdsDiagController@edit');
Route::delete('adsdiag/destroy/{id}','AdsDiagController@destroy');

Since Route::get('adsdiag/{id}/',AdsDiagController@create); is not generated by Resourcce so you need to include in your route explicitly.

Upvotes: 3

Joel Hinz
Joel Hinz

Reputation: 25384

You need to pass the arguments to the action() helper method in an array, even if it's just one argument:

action('AdsDiagController@create', [$record->id])

Upvotes: 0

Related Questions