Reputation: 544
I have 2 buttons in my blade. I want to update the same database column with them, when I click button 1, it updates the column to 1, when I click the second button it updates the column to 2. I am using the same route to do this because I have to pass the "id" from the view to the controller.
buttons in the view:
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Routes
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController@approveNotification')->name('show.approve_notification_application');
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController@sendNotificationToSuperAdmin')->name('show.approve_notification_application');
Controller
public function approveNotification($id){
$notification = Notification::find($id);
$notification->approved = '2';
$notification->save();
return redirect()->route('admin.notification_list');
}
public function sendNotificationToSuperAdmin($id){
$notification = Notification::find($id);
$notification->approved = '1';
$notification->save();
return redirect()->route('admin.notification_list');
}
I don't know how to do this. When I click any button, only the second route seems to work which means irrespective of which button I click, it always updates the table to with the value 1.
Upvotes: 1
Views: 847
Reputation: 732
The reason to your problem :
in the routes file - you called 2 methods with the same name - thats why it arrive to the 2nd route ( the seconed name overwrite the first one );
How to solve it ?
First - delete one of the routes.
Then - add an hidden field in your form, so you can know later which button was clicked
After that - you will need to add an IF in your controller - according to $id something like this :
if ($yourHiddenField == 1) {
... your code here...
} elseif ($yourHiddenField == 2 ) {
... your code here ...
}
( you will need to get the value of the hidden field first )
Upvotes: 1
Reputation: 1559
That's because you can't set two or more routes with same URL and method type. you can use same URL with other type like Route:get('hi')
and Route::post('hi')
.
Back to your problem you can do something like this:
Buttons
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 0) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 1) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Controller
public function approveNotification(Request $request, $id){
$notification = Notification::find($id);
$notification->approved = $request->input('type') == 1 ? 1 : 2;
$notification->save();
return redirect()->route('admin.notification_list');
}
don't forget to insert use Illuminate\Http\Request
in the top of the file after namespace.
Routes
keep the first and delete the second one.
Upvotes: 1