[英]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
列名: category
value : editorpicks
. 值: editorpicks
。
Why its not working. 为什么它不起作用。
I have tried it in tinker
also. 我也在tinker
尝试过。
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
. 请注意,您无需在where中使用“ =”,如果没有提供条件,则where
子句将默认为equals
。 get()
grabs the collection. get()
集合。 You could also do first()
to grab first single record, last()
, find($id)
, etc. 您还可以执行first()
来获取第一个单记录, last()
, find($id)
等。
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. 因此,在控制器顶部添加use App\\Recommend
(我假设这已经很有意义),然后只需使用$recommended = Recommend::where(...
保持环境整洁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.