Answers for "touches in laravel"

PHP
0

touches in laravel

/*
When a model has a BelongsTo or BelongsToMany relationship with another model, 
Let us say Comment that belongs to a Blog, it could in some cases be helpful
to update the parent’s timestamp when the child is updated.
This can be done by adding the relationship to the $touches attribute.
*/

class Comment extends Model
{
    protected $touches = ['blog'];

    public function blog()
    {
        return $this->belongsTo(App\Blog::class);
    }
}

/*
In Case we only want to Update One Model without needing change in other Model
ie Update updated_at without updating other fields
See the code here:
*/
public function updateUpdatedAt(){
  $user = User::find(1);
  $user->touch();
}
Posted by: Guest on March-31-2021

Code answers related to "touches in laravel"

Browse Popular Code Answers by Language