simone
simone

Reputation: 119

How to use different form with different controller functions and the same route in Laravel

I'm building an app with Laravel

In the same blade file I have different forms. In a div I have two forms.

In one form the user writes something in a textarea. After press the button ok, the page is reloaded and the data stored in a table of database.

The other form appears in a modal and it contains other option that the user can set and after press another button the data should stored in another table of database.

So how I can do this? I tried to route the function in my controller in this way:

Route::post('/anam','AnamnController@store');
Route::post('/anam','AnamnController@storePar');

and the tag form:

<form action="{{ action('AnamController@store') }}" method="post" class="form-horizontal">
<form id="formA" action="{{ action('AnamnesiController@storePar') }}" method="" class="form-horizontal">

But these return to me errors. So what I have to do?

Thanks!

Upvotes: 0

Views: 1173

Answers (2)

anasmorahhib
anasmorahhib

Reputation: 827

it's impossible to use the same route for two different functions unless you change one by get instead of post for example:

Route::post('/anam','AnamnController@store');
Route::get('/anam','AnamnController@storePar');

but you can make this logic:

Route::post('/anam','AnamnController@store');

and the tag form:

<form action="{{ action('AnamController@store') }}" method="post" class="form-horizontal">
    <input name="input_name" value="input-val" />
</form>

in controller:

public function store (){
   if(request()->input_name == 'val-1')
       this->store1();
   else
       this->store2();
}

protected function store1 () {
  //
}
protected function store2 () {
      //
}

Upvotes: 2

thisiskelvin
thisiskelvin

Reputation: 4202

You can't have two routes with the same url that has the same route method.

In your case you have two POST methods going to the same url. The second route definition is cancelling out the first.

Rename one of them.

Upvotes: 0

Related Questions