0

I am trying to query a table using where like :

Auth::user()
    ->friends
    ->where('friend.first_name','LIKE','%'.$firstName.'%')
    ->all()

The query does work properly without using the LIKE statement only when i add the LIKE keyword it doesn't return any results.

3
  • 1
    You need to better articulate your question, did it work before adding like? What happens when you use the column name only as the first parameter: Auth::user()->friends->where('first_name','LIKE','%'.$firstName.'%')->all(). What database are you using? Commented Sep 3, 2021 at 19:09
  • We need more explanation to understand your main point of the question Commented Sep 3, 2021 at 19:14
  • the where query works without adding LIKE it returns the results but when i add LIKE in the query it dosen't return anything Commented Sep 3, 2021 at 22:08

3 Answers 3

1

Using all() will work without the builder where() statement.

When grabbing data using the query, use get() instead:

Auth::user()->friends->where('something','LIKE','%'.$something.'%')->get()
Sign up to request clarification or add additional context in comments.

3 Comments

i replaced all() with get() i got this error : Too few arguments to function Illuminate\Support\Collection::get(), 0 passed
I don't know what your relations are, so I don't know what's coming in. If you have a collection already, then try without ->get(). IE see what happens with just the where statement - if you already have the DB pull for friends, the collection will return without the ->get(). But this is strange behavior.
Alternatively, you might be accessing friends as an attribute rather than a collection. You can try to query the relationship (assuming you have this relationship actually setup correctly in your user model): Auth::user()->friends()->where('first_name','LIKE','%'.$firstName.'%')->get(). Note the () after friends in this test.
0

I think you are confused between a laravel relation and a eloquent collection. First let me go straight to answer, you would have to do

Auth::user()
    ->friends()
    ->where('friend.first_name','LIKE','%'.$firstName.'%')
    ->get()

To explain what's wrong with your code, ->where('name', 'Like', '%something%') This is a query builder method. You have to use it on the query builder, or Before you actually retrieve the model / model collection.

Auth::user()->friends()

This would give you a relation Illuminate\Database\Eloquent\Relations\HasMany that you can apply where like on.


Auth::user()->friends

This will return you a Illuminate\Database\Eloquent\Collection. And yes, it is a collection, so you will have to use collection method. Take a look at the where method for collection. You can see it only support key => value pair. It does not support "LIKE"

The where method filters the collection by a given key / value pair:

If you want to use achieve a "Like" on collection, you can probably use the filter method, which support custom callback.

The filter method filters the collection using the given callback, keeping only those items that pass a given truth test:

To conclude, when you define a friends "HasMany" relation on User, you have two way to retrieve it.

If you want to apply additional query builder method, you need to use user->friends() which do not return you data, it return you a relation that you can use query builder method. Once you do user->friends, it get you a collection. You cannot apply query builder on that anymore. So basically, user->friends is act the same way as user->friends()->get()

Comments

0

Don't Use all() and use get().

Auth::user()->friends()->where('something','LIKE','%'.$something.'%')->get()

1 Comment

i replaced all() with get() i got this error : Too few arguments to function Illuminate\Support\Collection::get(), 0 passed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.