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

LaCoder's avatar

Softdelete query get results in Join query

Hello,

I have applied softDelete in Bill model and File model as well. But when we use SQL join, it also gets deleted items that have deleted_at timestamp,

$bills = Bill::where('is_bill', 1)
            ->join('files', 'files.bill_id', '=', 'bills.id')
            ->get()
            ->unique('bill_id');

i must need to use ->where('files.deleted_at', NULL) to remove those deleted items,

Was there an option to exclude those deleted in SQL query apart from writing this additional line?

As per DOC, the eloquent query will automatically exclude those, but with SQL?

0 likes
1 reply
bobbybouwmann's avatar

Yeah, Eloquent takes care of this, but with a join, you connect based on the database table and not based on the model. So in this case Eloquent doesn't know it needs to exclude the soft-deleted records.

Note that Eloquent is here for 90% of the most common cases. This is an edge case. You should just add the extra whereNull('files.deleted_at') in this case to make it work ;)

There are alternatives using relationships, but that is something different than using a join of course.

Please or to participate in this conversation.