Furkan ozturk
Furkan ozturk

Reputation: 722

How to take users from collection?

I have some codes like below

$journey = Journey::find($id);
$users = $journey->users()->whereHas('user',function ($query) use ($search) {
                $query->where('first_name', 'LIKE', "%{$search}%");
             })
             ->orderBy($sort, $order)
             ->paginate($limit);

I want to do with get method as follows;

$journeys = Journey::where('id',$id)->orWhere('duplicated_id',$id)->get();
$users = $journeys->users()->whereHas('user',function ($query) use ($search) {
                $query->where('first_name', 'LIKE', "%{$search}%");
            })
            ->orderBy($sort, $order)
            ->paginate($limit);

How to take users from a collection as above with pagination?

Upvotes: 0

Views: 66

Answers (2)

Octivia Bluekit
Octivia Bluekit

Reputation: 93

You can do this,

$journeyIds = Journey::where('id',$id)->orWhere('duplicated_id',$id)->pluck('id');
$userids = UserJourney::whereIn('journey_id',$journeyIds)->pluck('user_id');
$users = User::whereIn('id',$userids)
                ->where('first_name', 'LIKE', "%{$search}%")
                ->orderBy($sort, $order)
                ->paginate($limit);

Upvotes: 2

Kongulov
Kongulov

Reputation: 1171

use this code

$journeyIds = Journey::where('id',$id)->orWhere('duplicated_id',$id)->pluck('id');
$users = User::whereHas('user',function ($query) use ($search) {
            $query->where('first_name', 'LIKE', "%{$search}%");
        })
        ->whereIn('journey_id', $journeyIds) // <<-- your correct row
        ->orderBy($sort, $order)
        ->paginate($limit);

Upvotes: 0

Related Questions