Search code examples
phplaravellaravel-5.6laravelcollective

Do I have to fill my controller´s method when I use form model binding?


I'm doing and crud. I see in videos that in the form model binding use something like this:

{!! Form::model($usu, ['route' => ['prurequests.update', $usu->id],'method' => 'PUT']) !!}

but they have the update function empty.

i wanna know if i use prurequests.update or prurequests.edit or prurequests.store have i to fill these methods?

do I have to specify the method in if I put .store or .edit or .update because I see on the internet that they do not use it but I don't use it I get this:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

in my app i have the next:

{!! Form::model($usu, ['route' => ['prurequests.update', $usu->id],'method' => 'PUT']) !!}
  {!! Form::label('nombre', 'Modifique el nombre') !!}
  {!! Form::text('nombre', null) !!}
  {!! Form::label('slug', 'Modifique el slug') !!}
  {!! Form::text('slug', null) !!}
  {!! Form::submit('Actualizar') !!}
{!! Form::close() !!}

route:

Route::resource('/prurequests','PruebasControllers\PrurequestsController', ['parameters' => ['prurequests' => 'usuario2']]);

controller:

public function update(Request $request, $id)
 {
   $usuario = Usuario2::findOrFail($id);
   $usuario->update($request->all());
   return 'datos guardados';
 }

I don't know if I can let my update method empty


Solution

  • Insert Update and Delete is Easy When You are using laravel resource. Check my working Example

    In Edit method:

    public function edit($id)
        {
            $data['category'] = BlogCategory::find($id);
            return view('category.edit', compact(['data']));
        }
    

    In view :

    {!! Form::model($category, ['route' => ['category.update', $category->id],'method' => 'PUT']) !!}
      {!! Form::label('nombre', 'Modifique el nombre') !!}
      {!! Form::text('nombre', null) !!}
      {!! Form::label('slug', 'Modifique el slug') !!}
      {!! Form::text('slug', null) !!}
      {!! Form::submit('Actualizar') !!}
    {!! Form::close() !!}
    

    update method :

    public function update(Request $request, $id)
        {
    
            $data = $request->all();          
            $category = BlogCategory::find($id);
            $category->update($data);  
            return redirect(route('category.index'));
        }