laravel 拉拉维尔 | 雄辩的 foreach 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43271110/
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 | eloquent foreach not working
提问by Sapnesh Naik
I am trying to retrieve all non admin users from my user table. like so
我正在尝试从我的用户表中检索所有非管理员用户。像这样
$agents = User::where('is_admin','=', 'false')->get();
//didn't work
foreach ($agents as $agent=>$value)
{
echo "{$agent}=>{$value}"."<br>";
}
//tried dumping
dd($agents);
but It didn't work so I tried dumping the variable to check if it had any results, I have one non-admin as of now: and here is the output
但它不起作用所以我尝试转储变量以检查它是否有任何结果,到目前为止我有一个非管理员:这是输出
Collection {#219 ▼
#items: array:1 [▼
0 => User {#222 ▼
#casts: array:1 [?]
#fillable: array:6 [?]
#hidden: array:2 [?]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [?]
#original: array:10 [?]
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [?]
#rememberTokenName: "remember_token"
}
]
}
Please Help
请帮忙
回答by Big Boss
$agents = User::where('is_admin','=', 'false')->pluck('value', 'agent');
foreach ($agents as $agent=>$value) {
echo "{$agent}=>{$value}"."<br>";
}
Using pluck you will convert the object to a array, so you can use the foreach the way you want with the key => value
.
使用 pluck 可以将对象转换为数组,因此您可以按照自己的方式使用 foreach 和key => value
.
If you need to access other attributes of the model, you will need to do something like this:
如果您需要访问模型的其他属性,则需要执行以下操作:
$agents = User::where('is_admin','=', 'false')->get();
foreach ($agents as $agent) {
echo "{$agent->id}=>{$agent->name}"."<br>";
}
This way you just need to use $agent
and ->
followed by the attribute you want.
这样你只需要使用$agent
并->
跟随你想要的属性。
回答by lewis4u
Your controller should be like:
你的控制器应该是这样的:
public function index() {
$agents = User::where('is_admin','=', 'false')->get();
return view('viewfile', compact('agents'));
}
And then in that view file make the foreach loop in blade view
然后在该视图文件中使刀片视图中的 foreach 循环
@foreach ($agents as $agent)
{{ $agent->name }}
@endforeach
回答by Mayank Pandeyz
Here $agents
is a Std Class object array
. so access it elements try this:
这$agents
是一个Std Class object array
. 所以访问它的元素试试这个:
foreach ($agents as $agent)
{
$agent->name;
}
回答by chungvh
Try this code to echo information about all non-admins:
尝试使用此代码来回显有关所有非管理员的信息:
$agents = User::where('is_admin','=', 'false')->get();
foreach ($agents as $agent)
{
foreach($agent as $property => $value) {
echo "{$property} => {$value}"."<br>";
}
echo "<br/>----------------------<br/>";
}
回答by George G
What if you have a query like $items = items::all();
but then you want a specific value from the collection, like $items->barcode
?
如果您有一个类似的查询,$items = items::all();
但是您想要集合中的特定值,例如$items->barcode
?
When trying dd($items) the items are returned in an array form, which require for me to know each ID in order to get the appropriate result ($items[id]->barcode)
.
尝试 dd($items) 时,项目以数组形式返回,这要求我知道每个 ID 以获得适当的结果($items[id]->barcode)
。
I am guessing the answer would be having a foreach loop?
我猜答案是有一个 foreach 循环?