0

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.

0

2 Answers 2

3

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();
0
2

I am using it like this and it works fine. Try it like this and see if it helps.

$query = DB::table('your_table_name');
$query->where('name', 'LIKE', 'something');
$some_variable = $query->where('published', 1)->get();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.