PHP Snippet 1:
Schema::table('comments', function (Blueprint $table) {
...
$table->unsignedInteger('posts_id');
$table->foreign('posts_id')
->references('id')
->on('posts')
->onDelete('cascade');
});
PHP Snippet 2:
public function delete()
{
$this->images()->delete();
$this->comments()->delete();
return parent::delete();
}
PHP Snippet 3:
Post::findMany($ids)->each(function ($item) {
$item->delete();
});
PHP Snippet 4:
class Post extends Eloquent
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function images()
{
return $this->hasMany(Images::class);
}
public static function boot() {
parent::boot();
static::deleting(function($post) {
$post->comments()->delete();
$post->images()->delete();
// or call another method here after you declare above
});
}
}
PHP Snippet 5:
$post = Post::find($id);
$post->delete();