Search code examples
phplaravellaravel-routing

How to call controller update method from blade laravel?


form method="put" action="{{URL::action('siteController@update')}}" accept-charset="UTF-8"></form>
Route::post('site/update/{id}', 'siteController@update');
 public function update(Request $request, $id)
    {
        //

            $this->validate($request,[
            'Name'          => 'required',
            'Description'   => 'required',
            'Status'        => 'required'           
            ]);
            $Data               = site::find($id);
            $Data->Name         = $request->Name;
            $Data->Description  = $request->Description;
            $Data->Status       = $request->Status;
            if($Data->save())                   
            {
                return $this->index();
            }else{
                return redirect()->back()->withErrors($errors,$this->errorBag());
            }
    }

Solution

  • By adding a name to your route such as

    Route::post('site/update/{id}', 'siteController@update')->name('site-update');
    

    It allows you to generate its URL without knowing it at all

    <form method="post" action="{{ route('site-update', compact('id')) }}">
    @csrf
    add your form field here and use button type submit
    </form>
    

    Even if you decide to change the URL, the route helper does not care as long as the name stays the same (it's just an alias)