简体   繁体   中英

Query doesn't just get specific id

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM