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

tomasosho's avatar

...Eloquent\Collection::orWhere does not exist.

$userss = User::whereHas('roles', function ($q) {
                $q->where('name', 'manager');
            })->get();

            $q = $request->get ( 'q' );

            $users = $userss->where( 'name', 'LIKE', '%' . $q . '%' )->orWhere( 'email', 'LIKE', '%' . $q . '%' )->orWhere( 'phone', 'LIKE', '%' . $q . '%' )->get(20);
            // dd($users);

            if (count ( $users ) > 0)
                return view ( 'frontEnd.search.agent' , compact('users'))->withDetails( $users )->withQuery ( $q );
            else
                return view ( 'frontEnd.search.agent', compact('users'))->withMessage ( 'No Details found. Try to search again !' );
0 likes
13 replies
guybrush_threepwood's avatar

Hi @tomasosho

You should call get() after the orWhere() method:

            $q = $request->get('q');

            $users = User::whereHas('roles', function ($q) {
                $q->where('name', 'manager');
            })
            ->where('name', 'LIKE', '%' . $q . '%')
            ->orWhere('email', 'LIKE', '%' . $q . '%')
            ->orWhere('phone', 'LIKE', '%' . $q . '%')
            ->get(20);

            if (count($users)) {
                return view('frontEnd.search.agent' , [
                   'users' => $users,
                   'details' => $users, // Not sure why
                   'query' => $q,
                ]);
            }

            return view('frontEnd.search.agent', [
               'users' => $users,
               'message' => 'No Details found. Try to search again !'
            ]);
1 like
tomasosho's avatar

get wouldn't work, it's supposed to be paginate(20) and just as @snapey said, i can't run an sql statement on an eloquent collection.

guybrush_threepwood's avatar

Yeah, that was a typo. The point was, don't call get() or paginate() until you're done chaining your where() / orWhere() calls.

1 like
munazzil's avatar

According to this you can't take whatever the data you want you have to use paginate or take otherwise you can't render 20 data in the view, Use as like below,

for new laravel,

     $users = $userss->where( 'name', 'LIKE', '%' . $q . '%' )->orWhere( 'email', 'LIKE', '%' . $q . '%' )->orWhere( 'phone', 'LIKE', '%' . $q . '%' )->paginate(20);

and in the view use as like below before the end of </div>,

        {!!$users->links()!!}

for old method,

     $users = $userss->where( 'name', 'LIKE', '%' . $q . '%' )->orWhere( 'email', 'LIKE', '%' . $q . '%' )->orWhere( 'phone', 'LIKE', '%' . $q . '%' )->take(20)->get();
1 like
tomasosho's avatar

You're right, i made an error, it was supposed to be paginate(20)/take(20)->get() and not ```get(20)````

Snapey's avatar

Your $userss is an eloquent collection. you cannot run sql statements against a collection

1 like
Snapey's avatar
Snapey
Best Answer
Level 122
$users = User::whereHas('roles', function ($q) {
                    $q->where('name', 'manager');
            })->where(function($query) use($q) {
                    $query->where( 'name', 'LIKE', '%' . $q . '%' )
                         ->orWhere( 'email', 'LIKE', '%' . $q . '%' )
                         ->orWhere( 'phone', 'LIKE', '%' . $q . '%' );
              })->paginate(20);

1 like
tomasosho's avatar
ErrorException
Undefined variable: q on line 
$query->where( 'name', 'LIKE', '%' . $q . '%' )

Please or to participate in this conversation.