[英]Laravel query with primary key doesn't work
I have a users table with ID column as primary key. 我有一个用户表,其中ID列为主键。 I have this function in a Controller: 我在控制器中具有此功能:
public function show($id)
{
$items=user::all()->where('ID',$id);
foreach ($items as $item)
echo $item->Name;
}
This function doesn't echo anything. 此函数不响应任何内容。 If I change from $id to $name like this: $items=user::all()->where('Name',$name);
如果我将$ id更改为$ name,例如: $items=user::all()->where('Name',$name);
it returns what should return. 它返回应该返回的内容。 ID is primary key in the table, it's not a fillable field in the Model. ID是表中的主键,不是模型中的可填写字段。 If i put ID in the fillable array it doesn't work either. 如果我将ID放入可填充数组中,则也不起作用。 It doesn't work even if I hardcode the id like this $items=user::all()->where('ID','22');
即使我像这样硬编码$items=user::all()->where('ID','22');
What could be the problem ? 可能是什么问题呢 ?
all()
will run the query and return a collection of models, instead you should add the where
call first and then use get()
(or first()
) all()
将运行查询并返回模型集合,而应首先添加where
调用,然后使用get()
(或first()
)
$items = User::where('ID', $id)->get();
For more information, please read the official documentation 有关更多信息,请阅读官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.