Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yhoyoex/e7319c306ada3146d4fddc0f7f29345c to your computer and use it in GitHub Desktop.
Save yhoyoex/e7319c306ada3146d4fddc0f7f29345c to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Override parent boot and Call deleting event
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::deleting(function($users) {
foreach ($users->posts()->get() as $post) {
$post->delete();
}
});
}
/**
* Define relationship with post model
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function posts()
{
$this->hasMany('App\Post');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment