When I delete a user I want to delete all his records, for example, notifications, photos, posts, etc.
In my User model, I have hasMany relationship so how can I delete all the records automatically just by deleting the user?
Here is the user model
<?php
class User extends Eloquent
{
public function notifications()
{
return $this->hasMany('Notification');
}
public function photos()
{
return $this->hasMany('Photo');
}
public function posts()
{
return $this->hasMany('Post');
}
}
Here is the delete method.
$user = User::where('id', '=', $user_id);
if ( $user ->exists() ) {
$user->delete();
}