php 控制器中的 laravel foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24277443/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:15:27  来源:igfitidea点击:

laravel foreach loop in controller

phplaravelforeach

提问by thefalasifas

i have a problem about looping data in controller (laravel 4). my code is like this:

我有一个关于在控制器(laravel 4)中循环数据的问题。我的代码是这样的:

$owner = Input::get('owner');
$count = Input::get('count');
$product = Product::whereOwnerAndStatus($owner, 0)->take($count)->get();

when i want to use foreach to loop for $product result with code like this:

当我想使用 foreach 用这样的代码循环 $product 结果时:

foreach ($product->sku as $sku) {
    // Code Here
}

the result is returning error Undefined property: Illuminate\Database\Eloquent\Collection::$sku

结果返回错误 未定义属性:Illuminate\Database\Eloquent\Collection::$sku

so, i try to improvise a little with this code:

所以,我试着用这段代码即兴创作:

foreach ($product as $items) {
    foreach ($items->sku as $sku) {
        // Code Here        
    }
}

the code returning error like this: Invalid argument supplied for foreach()

代码返回这样的错误: 为 foreach() 提供的参数无效

is there someone who could help me solve this?

有人可以帮我解决这个问题吗?

回答by alex t

Hi, this will throw an error:

你好,这会抛出一个错误:

foreach ($product->sku as $sku){ 
// Code Here
}

because you cannot loop a model with a specific column ($product->sku) from the table.
So you must loop on the whole model:

因为您不能从表中循环具有特定列 ( $product->sku) 的模型。
所以你必须在整个模型上循环:

foreach ($product as $p) {
// code
}

Inside the loop you can retrieve whatever column you want just adding "->[column_name]"

在循环内,您可以检索您想要添加的任何列,只需添加“->[column_name]”

foreach ($product as $p) {
echo $p->sku;
}

Have a great day

祝你有美好的一天

回答by Ghanshyam Nakiya

The view (blade template): Inside the loop you can retrieve whatever column you looking for

视图(刀片模板):在循环内,您可以检索您要查找的任何列

 @foreach ($products as $product)
   {{$product->sku}}
 @endforeach

回答by The Alpha

Actually your $producthas no data because the Eloquentmodel returns NULL. It's probably because you have used whereOwnerAndStatuswhich seems wrong and if there were data in $productthen it would not work in your first example because get()returns a collection of multiple models but that is not the case. The second example throws error because foreachdidn't get any data. So I think it should be something like this:

实际上你$product没有数据,因为Eloquent模型返回NULL。这可能是因为您使用了whereOwnerAndStatuswhich 似乎是错误的,如果有数据,$product那么它在您的第一个示例中将不起作用,因为get()返回多个模型的集合,但事实并非如此。第二个例子抛出错误,因为foreach没有得到任何数据。所以我认为它应该是这样的:

$owner = Input::get('owner');
$count = Input::get('count');
$products = Product::whereOwner($owner, 0)->take($count)->get();

Further you may also make sure if $productshas data:

此外,您还可以确保是否$products有数据:

if($product) {
    return View:make('viewname')->with('products', $products);
}

Then in the view:

然后在view

foreach ($products as $product) {
    // If Product has sku (collection object, probably related models)
    foreach ($product->sku as $sku) {
        // Code Here        
    }
}

回答by Jake Wilson

Is skujust a property of the Productmodel? If so:

是否sku只是一个物业Product模型?如果是这样的话:

$products = Product::whereOwnerAndStatus($owner, 0)->take($count)->get();

foreach ($products as $product ) {
  // Access $product->sku here...
}

Or is skua relationship to another model? If that is the case, then, as long as your relationship is setup properly, you code should work.

或者是sku与另一个模型的关系?如果是这种情况,那么只要您的关系设置正确,您的代码就应该可以工作。