Hector Miguel Soto
Hector Miguel Soto

Reputation: 63

How do I call a Controller method in php Laravel?

I have a Controller and in it I have a several methods. My question is How do I set the Controller's action to my form:

<form action="action('ExamenController@InsertUser')" method="post">

Obviously this give the famous exception:

Action App\Http\Controllers\ExamenController@InsertUser not defined....blablabla

Also a tried with this:

<a class="btn btn-primary" href="{{action('ExamenController@InsertUser')}}">Save</a> 

The same error. Do anyone know how this works???? I don't understand Documentation from Laravel 5.5

But I know this works putting in web.php (previously route.php) using:

Route::post('url','ExamenController@InsertUser');

Upvotes: 0

Views: 2245

Answers (2)

parker_codes
parker_codes

Reputation: 3397

While using blade to merge in the route or url is handy, Laravel has a built-in way to handle this:

{!! Form::model($form, ['id' => 'my_form_id', 'method' => 'POST', 
'action' => ['ExamenController@InsertUser', $form->id], 'class' => 'some-class'])!!}

Upvotes: 0

James
James

Reputation: 16359

The correct syntax is to do it like so:

<form action="{{ action('ExamenController@InsertUser') }}" method="post">

This is well outlined in the docs.

This error, though:

Action App\Http\Controllers\ExamenController@InsertUser

Says that the method does not actually exist inside your controller.

Yes, you have defined a route for it with Route::post('url','ExamenController@InsertUser');, but have you actually created this method inside your ExamenController yet?

The error suggests you haven't and so I would double check that it exists and/or is spelt correctly.

An alternative, although this won't solve the issue if the InsertUser method doesn't exist would be to achieve what you're after like this:

<form action="{{ url('url') }}" method="post">

If you wanted to do this using named routes, then you can do this by providing a name to the route and then using that for your form action:

Route::post('url','ExamenController@InsertUser')->name('InsertUser');

<form action="{{ route('InsertUser') }}" method="post">

Which, again, is outlined in the docs.

Upvotes: 2

Related Questions