Home > Net >  How to add form action to laravel function
How to add form action to laravel function

Time:04-08

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 >
            <div >
                <div >
                    <h5 >@lang('Courier Location')</h5>
                    <div >
                        <form action="{{route('....')}}" method="POST">
                                @csrf
                                <div >
                                    <div >
                                        <label for="current_location" >@lang('Current Location')</label>
                                        <input type="text"  name="current_location" value="{{__($courierInfo->current_location)}}" required="">
                                    </div>
            
                                    
            
                                </div>
                                <div >
                                    
                                    <button type="submit" ><i ></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.

CodePudding user response:

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', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location', 'YourController@courierUpdate')->name('updateLocation');

And then in your blade view

<form action="{{ route('updateLocation') }}" method="POST">
 @csrf
</form>

CodePudding user response:

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)

  • Related