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

engrlaravel's avatar

Laravel Eloquent query OR problem

I have a query

$tasks = Task::where('deleted_at', null)->where('company_id',$company_id);
$tasks = $tasks->where('created_by',$user_id);
$tasks = $tasks->orWhereIn('id',$task_ids);

It generate below query

SELECT * FROM `tasks` WHERE `deleted_at` IS NULL AND  `company_id` = 25 AND `created_by` = 20 OR 
`id` IN(112,...215) ORDER BY `id` DESC

Now Id 112 is deleted but still showing in result, although i have where('deleted_at', null) condition but it is not working

how to apply that as well?

0 likes
11 replies
MichalOravec's avatar

It could be like this

$tasks = Task::where('company_id', $company_id)->where('created_by',$user_id)->orWhere(function ($query) use ($task_ids) {
    $query->where('deleted_at', null)->whereIn('id', $task_ids);
})->get();
engrlaravel's avatar

@michaloravec : I want OR condition.

Example: select records which is created by me OR i am involved in that.

whole query is like this

        $tasks = Task::where('deleted_at', null)->where('company_id',$request->input('company_id'));
        $tasks = $tasks->where('created_by',$request->input('user_id'));
        $task_ids = TaskUser::where('user_id',$request->input('user_id'))->pluck('task_id')->all();
        $tasks = $tasks->orWhereIn('id',$task_ids);
MichalOravec's avatar
$tasks = Task::where(function ($query) use ($company_id, $user_id) {
    $query->where('company_id', $company_id)->where('created_by', $user_id);
})->orWhere(function ($query) use ($task_ids) {
    $query->where('deleted_at', null)->whereIn('id', $task_ids);
})->get();
engrlaravel's avatar

@michaloravec : Now it will show deleted records for created_by by me. because deleted_at condition applied only on

id IN(...)

MichalOravec's avatar

@engrlaravel So add it where you need it.

$tasks = Task::where(function ($query) use ($company_id, $user_id) {
    $query->where('company_id', $company_id)->where('created_by', $user_id)->where('deleted_at', null);
})->orWhere(function ($query) use ($task_ids) {
    $query->where('deleted_at', null)->whereIn('id', $task_ids);
})->get();
MichalOravec's avatar

@engrlaravel This also should work

$tasks = Task::where('company_id', $company_id)->where('deleted_at', null)->where(function ($query) use ($user_id, $task_ids) {
    $query->where('created_by', $user_id)->orWhereIn('id', $task_ids);
})->get();
engrlaravel's avatar

@all : Unfortunately this is not working, I am expecting a proper optimized query for laravel experts.

I want to apply all conditions on $task_ids as well

MichalOravec's avatar

It's just pure logic, so you have to thinking more, otherwise you can pay to someone for it.

MichalOravec's avatar

Result of this

$tasks = Task::where('deleted_at', null)->where(function ($query) use ($company_id, $user_id, $task_ids) {
    $query->where(function ($query) use ($company_id, $task_ids) {
        $query->where('company_id', $company_id)->where('created_by', $user_id);
    })->orWhereIn('id', $task_ids);
})->get();

Should be same like this

$tasks = Task::where(function ($query) use ($company_id, $user_id) {
    $query->where('company_id', $company_id)->where('created_by', $user_id)->where('deleted_at', null);
})->orWhere(function ($query) use ($task_ids) {
    $query->where('deleted_at', null)->whereIn('id', $task_ids);
})->get();

So there is no better solution.

Please or to participate in this conversation.