Problem with created_at / updated_at when updating record in laravel 5.6

1

I am programming the CRUD of the user (modified) in my laravel project (5.6) but when updating a user it conflicts with the registration / update dates, or at least that is what I have understood after reviewing several queries on the web.

Mi  update: 
 /**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function update(Request $request)
{
    $usuario = User::find($request['id']);
    $usuario->id_role = $request['id_role'];
    $usuario->name = $request['nom_usuario'];
    $usuario->last_name = $request['ape_usuario'];
    $usuario->rut_user = $request['rut_usuario'];
    $usuario->telefono1 = $request['telefono1'];
    $usuario->telefono2 = $request['telefono2'];
    $usuario->email = $request['ema_usuario'];
    $usuario->save();
    return Redirect::to('/usuarios')->with('notice', 'El usuario ha sido modificado correctamente.');
}

My model:

<?php

 namespace App;

 use Illuminate\Notifications\Notifiable;
 use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function getListado(){

    $usuarios = $this->join('roles', 'users.id_role', '=', 'roles.id_role')
                ->select('users.*', 'roles.des_role')
                ->orderBy('name', 'ASC');
    return $usuarios;

}
}  

When adding in the model the     public $ timestamps = false;
Saves but does not record the update date (which is the purpose of that statement) However, that also need to be saved, obviously.

I appreciate any contribution.

I have been resorting to this resource very often lately, I think it is the most healthy when we are learning a new tool and we can not find the indicated answer. I appreciate all the guidelines.

    
asked by Virginia 08.08.2018 в 18:57
source

3 answers

0

Try adding in the update:

public function update(Request $request)
{
    $usuario = User::find($request['id']);
    $usuario->id_role = $request['id_role'];
    $usuario->name = $request['nom_usuario'];
    $usuario->last_name = $request['ape_usuario'];
    $usuario->rut_user = $request['rut_usuario'];
    $usuario->telefono1 = $request['telefono1'];
    $usuario->telefono2 = $request['telefono2'];
    $usuario->email = $request['ema_usuario'];
    $usuario->updated_at = Carbon\Carbon::now(); //Agregas la fecha de actualización
    $usuario->save();
    return Redirect::to('/usuarios')->with('notice', 'El usuario ha sido modificado correctamente.');
}
    
answered by 09.08.2018 в 00:09
0

You should not be manually updating your updated_at or created_at.

If you make a modification and the model does not change, the updated_at will not update , but you could perfectly, if for X reason you want to force that change , do this:

$tuModelo->touch();

This updates the updated_at.

Now, another thing that worries me in what I see, is that you show the fillable of User and you have this:

protected $fillable = [
    'name', 'email', 'password',
];

That means, that if you use the -> save (); Any other property of your model will not be updated because it is not in the fillable. And that could be causing not to actually update your model (in case you do not > name , or email or password have changed) and that is why the updated_at does not change.

I advise you to add the other properties of the model (or columns of the table) that will be allowed to modify, to the $ fillable. You would have something like this:

protected $fillable = [
    'name',
    'email',
    'password',
    'id_role',
    'last_name',
    'rut_user',
    'telefono1',
    'telefono2'
];

Or something like that according to your real fields that I do not know.

The only thing I have left is to ask you (but I doubt that is the problem) is how you created updated_at in your migration. Did you create it that way?

$table->timestamps();

With that you create the created_at and the updated_at. The important thing is that they are dateTimes or timestamps so you do not have problems.

    
answered by 09.08.2018 в 15:31
0

I share the solution, maybe it will help someone else ::: Effectively!!! The problem with sql server is that the data type "timestamp" as such does not exist, when creating a table, either by migrations or sql, the server takes it as datetime, so that laravel works correctly, the data type for all the date fields must be datetime2 ...

Thank you all for your contributions !!!

We continue to learn:)

    
answered by 10.08.2018 в 14:59