'How to add form action to laravel function
I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">@lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
@csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">@lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>@lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Solution 1:[1]
Declare a route on the web.php Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName@courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier like this: route('courier.Update',$courier->id)
Solution 2:[2]
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController@courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
@csrf
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Md Abir Hossain |
| Solution 2 |
