Sami
Sami

Reputation: 141

Laravel "->where(like) issue"

I have this code

$users = array();
$users_to_add = User::all()->where('unit', 'LIKE', '%'.$request->unit.'%');

But i get nothing on $users_to_add. The like doesn't work. What am i missing?

Upvotes: 0

Views: 72

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35367

all() actually runs a query to get all users. Therefore, your where() call is on the Collection (PHP), not the Query Builder (SQL).

You'll want to use get() but make sure you declare your query conditions prior to calling it.

get() will execute the query, with your defined conditions, and return a collection of results.

User::where('unit', 'LIKE', '%'.$request->unit.'%')->get();

Upvotes: 1

Related Questions