The controller does not receive a laravel argument 5.3

0

In the view:

{!! Form::model($evento,['route'=>['eventos.update',$evento->id], 'method'=>'PUT','files'=> true])!!}
@include('eventos.form.editar')
{!! Form::submit('Editar',['class'=>'btn btn-primary']) !!}
{!! Form::close() !!}

In the controller:

    public function update($id, Request $request)
{
    $evento = Evento::find($id);
    $evento->fill($request->all());
    $evento->save();
    Session::flash('mensaje','Evento editado Correctamente');
    return Redirect::to('eventos');
}

On the route:

Route::resource('eventos','EventoController');

I get the following error:

FatalThrowableError in EventoController.php line 54:
Type error: Too few arguments to function App\Http\Controllers\EventoController::update(), 1 passed and exactly 2 expected

The id seems to send it but the request does not ... I do not know why that happens ...

    
asked by Susje 17.01.2018 в 02:33
source

2 answers

0

I managed to solve, in case someone serves or wants to improve my solution. For some reason the controller is not receiving the variable $ id, so what I did was create a hidden type input with the value of the record id and with a $ request take that id and look for the record to be able to edit later.

I remain like this:

Controller:

    public function update(Request $request)
{
    $evento = Evento::find($request->eventId);
    $evento->nombre = $request->nombre;
    $evento->descripcion = $request->descripcion;
    $evento->img = $request->img;
    $evento->fecha = $request->fecha;
    $evento->lugar = $request->lugar;
    $evento->precio = $request->precio;
    $evento->estatus = $request->estatus;
    $evento->descuento_registro = $request->descuento_registro;
    $evento->slug = $request->slug;
    $evento->tipo_evento = $request->tipo_evento;
    $evento->evento_privado = $request->evento_privado;
    $evento->save();
    Session::flash('mensaje','Evento editado Correctamente');
    return Redirect::to('eventos');


}

View:

<input type="hidden" value="{{$evento->id}}" name="eventId">
    
answered by 17.01.2018 в 06:37
0

The problem is that you must first inject the dependencies and then the rest of the parameters.

Your example:

public function update($id, Request $request)
{
    //
} 

It should look like this:

public function update(Request $request, $id)
{
    //
} 

Documentation: link

    
answered by 17.01.2018 в 09:43