When I'm trying to fire query without chaining, my browser gets hanged - no response/error at all, but query with chaining works fine. I have read at many places that Laravel allows query even without using chaining. But don't know why its not working for me.
And by the way its just a demo project, so there is only 1 table in database named "users" having 50 records in that, so its not like that that browser gets hanged due to high amount of data returned as a query result.
Not working:
$users = User::where('first_name', 'LIKE', '%Leonie%');
$users -> get();
Working:
$users = User::where('first_name', 'LIKE', '%Leonie%') -> get();
I'm using latest Laravel version 5.2. All things are configured properly like database, application configuration etc..
Any help would be appreciated.
Thanks.
When you don't call get()
or first()
(amongst a few others) for example which get a result set, you end up just with a Builder
object. This is the object which is used to construct queries. In your first example, you assign a Builder
object to $users
, but in your second, you assign a Collection
object.
// This assigns the result to $users
$users = User::where('first_name', 'LIKE', '%Leonie%') -> get();
// Doesn't assign the result to $users
$users = User::where('first_name', 'LIKE', '%Leonie%');
$users -> get();
You would need to do
$users = User::where('first_name', 'LIKE', '%Leonie%');
$users = $users->get(); // Note the $users =
It may be better to name the variable that which is used to store the query builder object something more obvious; such as $query
perhaps.
$query = Users::where();
$users = $query->get();