0

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');
2
  • Why don't you use named routes? Commented Oct 20, 2017 at 1:18
  • How does it works? I understand nothing from laravel documentation Commented Oct 20, 2017 at 1:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Please post your controller in your question.
public function InsertUser(Request $request) { DB::table('usuarios')->insert([.......
Hector, add it to your question, not as a comment.
Nice, the last one works, using names. Guys sorry but I'm Naive programmer
0

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'])!!}

1 Comment

Really? I didn't know about this. I will try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.