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

heyIm's avatar
Level 1

I need following output of query using laravel.

table-1

id

1

table-2

fk_id deleted_at

1 null

1 not null

1 not null

output :- Not Need any output because id-1 one record are not deleted.

In short I need all those id that all records are deleted like this,

table-1

id

1

2

table-2

id fk_id deleted_at

1 2 not null

2 2 not null

3 2 not null

4 1 null

5 1 not null

6 1 not null

output:- I need id-2 must be in output because all record of id-2 is deleted.

I need above id found result using laravel Eloquent query.

This is my query code :-

$query = table-1->select('table1.id as table_1_id','table2.id as table_2_id') ->leftJoin('table2', 'table2.fk_id', '=', 'table1.id') ->whereNull('table2.id') ->whereNull('table2.deleted_at')

                ->orWhere(function($query)
                {
                    $query->whereNotNull('table2.deleted_at')
                          ->where('table2.deleted_at','!=',NULL);
                })
                ->get()
                ->toArray();   

But i am still not getting expected output

0 likes
4 replies
tykus's avatar

Can you rephrase you question, and wrap any code snippets properly in 3 backticks (```)?

click's avatar

What tykus says, your question is not clear.

But one thing: if this is an actual mysql query, if you want to check on NULL values you should compare it with IS and not = like: deleted_at IS NULL and not deleted_at = NULL or deleted_at != NULL

1 like
heyIm's avatar
Level 1

@tykus, Can i give new line in question if yes so how can? In question i have write table structure and that structure show in one line so its showing confusing.

Please or to participate in this conversation.