trashed () returning 'true' after restore ()?

2

I have a Manifestation template that uses SoftDeletingTrait to keep them inactive in the database and in ManifestationController I have the following destroy() method:

$manifestation = Manifestation::findOrFail($id);
$manifestation->closedBy()->associate(Auth::user());
$manifestation->save(); // save para marcar o usuário que encerrou
$manifestation->delete();

And if right after $manifestation->trashed() , it returns true , and so far everything is correct. However, I also have a method restore() in controller , with the following:

$manifestation = Manifestation::withTrashed()->findOrFail($id);
$manifestation->closed_by_id = null;
$manifestation->save();
$manifestation->restore();

The problem is that if I use $manifestation->trashed() this time, I keep getting true as a result, even though the model is no longer inactive.

  • Is this behavior correct?
  • Is there a way to work around and cause trashed() to return false after restore() ?
  • If I'm not mistaken, there must be flag , as well as exists of class Eloquent that can possibly be changed, but I could not find it. I could, instead of using trashed , check that the deleted_at value is null , but I want to avoid doing so, unless it is the last opcion.

    Notes :

    • It returns to normal search results.
    • I'm using Laravel 4.2 and I can not change.
    asked by anonymous 16.01.2017 / 19:28

    1 answer

    1

    After commenting on the Virgilio I discovered the which caused the problem. I created another template with the same trait and controller and it worked without problems, so the problem was in the Manifestation template.

    What was making the result always true was that you were formatting the deleted_at attribute in a readable format to be used in view using the% , and this made it never null , it was the starting date (January 1, 1970) even though it was null in the database, and strftime simply Check this (if the value is null).

    To resolve, it was only necessary to create a new attribute of another name by calling trashed , formatting it only in the new attribute and start using it thereafter.

    It was somewhat similar to:

    public function getDeletedAtAttribute($data){
      return strftime('%d/%m/%Y às %H:%M', strtotime($data));
    }
    

    After correction:

    # getDeletedAtAttribute() removido
    
    public function getClosedAtAttribute(){
      $data = $this->deleted_at;
      return strftime('%d/%m/%Y às %H:%M', strtotime($data));
    }
    
        
    16.01.2017 / 23:16