laravel 拉拉维尔。使用查询生成器获取表的单列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45250772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel. Get single column values of a table using Query Builder
提问by Jan Ariel San Jose
I have a table named "items" and an input for the where condition named "ref_code".
我有一个名为“items”的表和一个名为“ref_code”的 where 条件的输入。
$items = DB::table('items')
->where('ref_code','=', $request->ref_code)
->get(['id', 'ref_code', 'name','price']);
But I can't seem to take the values of each column.
但我似乎无法获取每一列的值。
I checked wether the query builder worked or not by using:
我使用以下方法检查了查询构建器是否工作:
return $items;
Fortunately, there's no problem.
幸运的是,没有问题。
But returning or getting a single value doesn't work with:
但是返回或获取单个值不适用于:
return $items->id
Is my syntax wrong? All of this are inside my controller.
我的语法错了吗?所有这些都在我的控制器中。
EDIT: I tried
编辑:我试过了
dd($items);
before returning and it showed me this:
在返回之前,它向我展示了这个:
Collection {#325 ▼
#items: array:1 [▼
0 => {#322 ?}
]
}
回答by Md. Abu Taleb
Thanks for updating your question with the result. Look at your debug result. it looks like
感谢您使用结果更新您的问题。看看你的调试结果。看起来像
array:1 [▼
0 => {#322 ?}
]
That means your query returning a collection of arrays because of you're using get()
method. so get()
method always return a collection of an array.
这意味着您的查询将返回一组数组,因为您正在使用get()
方法。soget()
方法总是返回一个数组的集合。
For avoiding this problem you have to use first()
method instead of get()
.
Remember: all the time you have to use first() method when you want to get a single row.
为了避免这个问题,你必须使用first()
method 而不是get()
.
请记住:当您想要获取单行时,您必须始终使用 first() 方法。
So your query should be like :
所以你的查询应该是这样的:
$items = DB::table('items')
->select('id', 'ref_code', 'name', 'price')
->where('ref_code','=', $request->ref_code)
->first();
Or
或者
$item = YourModelName::select('id', 'ref_code', 'name', 'price')
->where('ref_code','=', $request->ref_code)
->first();
And finally get output as like$item->id, $item->ref_code etc.
最后得到像$item->id、$item->ref_code 等的输出。
Hope it will help.
希望它会有所帮助。
References:https://laravel.com/docs/5.4/queries#retrieving-results
参考资料:https : //laravel.com/docs/5.4/queries#retrieving-results
回答by linktoahref
get()
would return a collection
get()
会返回一个集合
$items = DB::table('items')
->where('ref_code','=', $request->ref_code)
->get(['id', 'ref_code', 'name','price']);
In the above case $items
would be a collection, hence you need to loop through the collection to access the properties
在上述情况下$items
将是一个集合,因此您需要遍历集合以访问属性
foreach ($items as $item) {
$item->price;
}
If you'd need to return a Model instance you could use the method first()
instead
如果你需要返回Model实例,你可以使用的方法first()
,而不是
$items = DB::table('items')
->select('id', 'ref_code', 'name', 'price')
->where('ref_code','=', $request->ref_code)
->first();
and access the properties as
并访问属性
$items->price;
回答by sanjiv pandey
Try this using model
试试这个使用模型
$result = Model_name::->where('ref_code','=', $request->ref_code)
->first(['id', 'ref_code', 'name', 'price']);