In your Thread.php add this approch
// Thread.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model
{
protected $guarded = [];
protected $withCount = ['replies'];
// Methods
public function path()
{
return url('threads/' . $this->id);
}
// Relationships
public function activities()
{
return $this->morphMany(Activity::class, 'subject');
}
public function channel()
{
return $this->belongsTo(Channel::class);
}
public function replies()
{
return $this->hasMany(Reply::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
protected static function boot() {
parent::boot();
static::deleting(function($thread ) {
$thread ->replies()->delete();
$thread ->activities()->delete();
// if any other dependents delete here
});
}
}
Now in your controller just
// ThreadController.php
public function destroy(Thread $thread)
{
$this->authorize('delete', $thread);
$thread->delete();
return redirect('threads');
}