form update with request in laravel 5.3

0

I have This form that updates the user table, but it throws this error:

ERROR MESSAGE: Creating default object from empty value

The Code of the Form:

    <h1 class="text-center text-uppercase">Formulario de Edicion</h1>

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

        <div class="form-group">

        {!!Form::token()!!}

        <div class="form-group">

            {!!Form::label('nombre', 'Nombre', ['class' => 'awesome'])!!}
            {!!Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Ingresa tu nombre'])!!}

        </div>
        <div class="form-group">

            {!!Form::label('email', 'Email', ['class' => 'awesome'])!!}
            {!!Form::text('email', null, ['class' => 'form-control', 'placeholder' => 'Ingrese su correo'])!!}

        </div>
        <div class="form-group">

            {!!Form::label('password', 'Passwork', ['class' => 'awesome'])!!}
            {!!Form::password('password', ['class'=>'form-control', 'placeholder'=>'Ingrese su passwork'])!!}

        </div>
        <br>

        <div class="form-group">
            {!!Form::submit('Actualizar', ['class'=>'btn btn-primary'])!!}
        </div>
    </div>

The Controller Code:

    public function update(Request $request, $id)
    {
        //
        $users = User::find($id);
        $users->name = $request->name;
        $users->email = $request->email;
        $users->password = bcrypt($request->password);
        $users->save();
        return redirect('admin')->with('mensaje', 'Usuario actualizado correctamente');
    }

Thanks for answering, keep giving the same error even doing it like this:

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

    $users = User::find($id);
    $users->name =$request->get('name');
    $users->email = $request->get('email');
    $users->password = bcrypt($request->get('password'));
    $users->save();
    return redirect('admin')->with('mensaje', 'Usuario actualizado correctamente');
}
    
asked by Gali 27.02.2017 в 02:41
source

1 answer

2

I see two problems.

  • in your view: {!!Form::model($usuarios, ['route'=> ['admin.update', '$usuarios->id'], 'method'=>'PUT'])!!} . $usuarios->id is within quotation marks, so when you do the PUT, it's not with the user's id but with the value "$usuarios->id" .
  • Due to the previous problem, your variable $users is NULL whereby you can not edit its properties, launching the error you indicate: Creating default object from empty value
  • Specifically, this error refers to what I said earlier, try to modify a null variable, as if it were an object, for example:

    → cat null.php
    <?php
    $obj = NULL;
    
    $obj->a = 'a';
    $obj->b = 'b';
    
    → php null.php
    PHP Warning:  Creating default object from empty value in <path>/null.php on line 5
    

    greetings

        
    answered by 27.02.2017 в 13:06