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

JerodevS's avatar

Laravel query is not working properly

I have a query to get all logged activity on a folder.

The ID in this case is 2. I'm trying to get just records on 2, but as I have orwhere for search results, it gets all activity accross all folders for ths user.

Any idea how to fix this query?

$filtertext = $request->filter;
    $data = Activity::select('activity_log.*', 'users.first_name', 'users.last_name', 'users.email')
        ->join('users', 'users.id', '=', 'activity_log.causer_id')
        ->where('subject_id', '=', $id)
        ->where('subject_type', '=', 'App\Models\Node')
        ->orWhere('first_name', 'like', '%' . $filtertext . '%')
        ->orWhere('last_name', 'like', '%' . $filtertext . '%')
        ->orWhere('email', 'like', '%' . $filtertext . '%')
        ->with([
            'subject' => function ($q) {
                $q->whereNull('deleted_at');
            }
        ])
        ->orderBy('created_at', 'desc')
        ->paginate(12);
1 like
6 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@jerodevs

When you have a group of orwhere's in combination with normal where's, you should put them in their own closure so the query builder knows these need to be seen together apart from the other where's.

$filtertext = $request->filter;
$data = Activity::select('activity_log.*', 'users.first_name', 'users.last_name', 'users.email')
    ->join('users', 'users.id', '=', 'activity_log.causer_id')
    ->where('subject_id', '=', $id)
    ->where('subject_type', '=', 'App\Models\Node')
    ->where(function ($query) use ($filtertext) {
        $query
            ->orWhere('first_name', 'like', '%' . $filtertext . '%')
            ->orWhere('last_name', 'like', '%' . $filtertext . '%')
            ->orWhere('email', 'like', '%' . $filtertext . '%');
    })
    ->with([
        'subject' => function ($q) {
            $q->whereNull('deleted_at');
        }
    ])
    ->orderBy('created_at', 'desc')
    ->paginate(12);
11 likes
xpaceseven's avatar

The where condition are not used correctly, could you try this:

$career_solution = CareerSolution::with('user.role', 'user.privancy_setting', 'category', 'sub_category', 'country');

$career_solution = $career_solution->where(function ($query) { $query->where('expires_at', '>=', date('Y-m-d')) ->orWhere('expires_at', '=', '0000-00-00'); });

$career_solution = $career_solution->where(function ($query1) use ($id) { $query1->where(function ($query2) use ($id) { // assuming that the relation name in User model is name public function privacy_setting .. $query2->whereHas('user.privacy_setting', function ($query3) { $query3->where('career_solutions', '=', 0); // You don't need to specify the table here only the table field you want })->where('public', '=', 1); })->orWhere(function ($query4) use ($id) { // idem here the relation must be name contact $query4->whereHas('user.contact', function ($query5) use ($id) { $query5->where('user_id', '=', $id); // idem here })->orWhere('user_id', '=', $id); }); }); https://jiofilocalhtml.run https://forpc.onl

2 likes
rareskyfour's avatar

Hey i am also facing same issue if someone could help me with correct coding it will be a great help.

Thank you.!

2 likes

Please or to participate in this conversation.