Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

timokfine's avatar

Cannot delete replies on a thread?

I'm following the Let's Build a Forum with Laravel and TDD series and have hit a roadblock.

When a thread is deleted, I would like to delete all of that thread's associated replies. But, the replies are not being removed from my database.

I have no issues displaying a thread's replies. They simply won't delete.

What have I missed here?

Quick overview of my code:

// ThreadController.php

public function destroy(Thread $thread)
{
    $this->authorize('delete', $thread);

    $thread->activities()->delete();
    $thread->replies()->delete();

    $thread->delete();

    return redirect('threads');
}
// 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);
    }
}
0 likes
5 replies
RamjithAp's avatar

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');
}
timokfine's avatar

Thanks, but it doesn't matter where I put the following line, it's simply not deleting any associated replies.

$thread->replies()->delete();

I've tried adding it directly to the controller, to an event, to an observer, etc.

timokfine's avatar

Ok. After some trial and error, I have found the culprit.

// Reply.php
protected $withCount = ['likes'];

This line is preventing the replies being deleted. Comment this line out, and the records are deleted.

Weird.

timokfine's avatar

I've removed $withCount from my models and have added it to my model queries instead. No issues now.

Please or to participate in this conversation.