상위 모델의 timestamp(updated_at) 갱신하기
https://laravel.com/docs/11.x/eloquent-relationships#touching-parent-timestamps
https://panjeh.medium.com/touch-laravel-model-touch-relationships-in-depth-explanations-822b66d87fa8
$article = Article::find($id);
$article->touch();
use Illuminate\\Database\\Eloquent\\Model;
class Comment extends Model
{
protected $touches = ['post'];
public function post()
{
return $this->belongsTo('App\\Post');
}
}
사용할 때 주의
https://blog.creekorful.org/2021/11/laravel-beware-of-touches/
Define the UserSaved event
class User extends Model
{
protected $dispatchesEvents = [
'saved' => UserSaved::class,
];
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}
}
Define the UserListener
class UserListener
{
public function handleUserSaved(UserSaved $event)
{
// Take roles ids by batch of 1000 and run a single SQL query
// to bump updated_at.
$event->user->roles()->chunk(1000, function (Collection $role) {
Role::whereIn('id', $role->pluck('id'))->update(['updated_at' => Carbon::now()]);
});
}
}