Yellow Yellow - 5 months ago 10
PHP Question

Laravel eloquent where clause issue

I have the following structure of the table:

`id` int(10) UNSIGNED NOT NULL,
`user_id` int(10) UNSIGNED NOT NULL,
`order` int(11) NOT NULL,
`category` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL


I have five confirm records in table where i am querying table like this :

$recommended = App\Recommend::where('category', '=', 'editorpicks');


But the result comes empty, Let me paste the column name and value against it straight from DB.

column name :
category


value :
editorpicks
.

Why its not working.

I have tried it in
tinker
also.

Answer
App\Recommend::where('category', 'editorpicks')->get();

Note, you don't need to use "=" in where, if no conditional is provided, the where clause will default to equals. get() grabs the collection. You could also do first() to grab first single record, last(), find($id), etc.

It's also good practice to namespace the model as well. So add use App\Recommend to top of controller (I'll assume this already makes sense) and then just use $recommended = Recommend::where(.... Keep things clean.