Skip to content

Instantly share code, notes, and snippets.

@nwaughachukwuma
Last active March 28, 2020 21:45
Show Gist options
  • Save nwaughachukwuma/8620b5138744b53f3fd0a09ebed374ad to your computer and use it in GitHub Desktop.
Save nwaughachukwuma/8620b5138744b53f3fd0a09ebed374ad to your computer and use it in GitHub Desktop.
A post model with it's relationships
<?
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('\App\User');
}
public function comments()
{
return $this->hasMany('\App\Comment');
}
// this is a recommended way to declare event handlers
public static function boot() {
parent::boot();
self::deleting(function($post) { // before delete() method call this
$post->comments()->each(function($comment) {
$comment->delete(); // <-- direct deletion
});
// do the rest of the cleanup...
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment