Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用foreach循环的Laravel集合迭代_Php_Laravel_Collections_Foreach - Fatal编程技术网

Php 使用foreach循环的Laravel集合迭代

Php 使用foreach循环的Laravel集合迭代,php,laravel,collections,foreach,Php,Laravel,Collections,Foreach,这是使用订单模型在控制器中进行的简单查询: $orders = Order::where('user_id', auth()->user()->id) ->orderBy('created_at', 'desc') ->get(); return dd($orders); dd给出了以下结果:(哪一个是正确的) 但是当我遍历$orders集合foreach循环时,只显示第一个

这是使用订单模型在控制器中进行的简单查询:

$orders = Order::where('user_id', auth()->user()->id)
                        ->orderBy('created_at', 'desc')
                        ->get();
return dd($orders);
dd给出了以下结果:(哪一个是正确的)

但是当我遍历$orders集合foreach循环时,只显示第一个数组,第二个数组不可访问。。。foreach循环有什么问题

$temp='';
        foreach($orders as $order){
                $temp.= $order->product; // accessing the belongTo method
        }

        dd($temp);
以下是输出(仅显示一个数组):


如果要跨所有订单构建产品数组,则不能使用字符串连接。使用阵列并将项目推送到阵列上

$products = [];
foreach ($orders as $order) {
    if ($order->product) {
        $products[] = $order->product;
    }
}
dd($products); // will be an array of all other products.
试试这个:

$temp= [];
foreach($orders->all() as $order){
     $temp[] = $order->product;
}
您可以编辑您的查询

$orders = Order::where('user_id', auth()->user()->id)
                    ->with('products')
                    ->orderBy('created_at', 'desc')
                    ->get();

谢谢,但这仍然会使第二个数组项为空。。。这是dd的输出<代码>数组:2[▼ 0=>乘积{#276▶} 1=>null]我只是想知道foreach循环有什么问题……或者我在这里遗漏了什么……第二项的
null
项意味着
产品
没有定义。我更新了代码以避免这不是问题……我也得到了相同的结果,请参见
dd($temp)
有问题。foreach循环假设正好循环通过2个产品。目前我不知道为什么它成功地循环通过第一个产品,但在第二次迭代中它给出null…(两个产品都存在)我认为这与laravel集合有关…可能是我没有正确地遍历集合…代码没有变化…到目前为止,我的问题中的代码和您的答案都给出了相同的结果…但都忽略了集合中的第二项…这是真正的问题?显示模型定义。
类产品扩展模型{use SoftDeletes;/***应该变异为日期的属性。**@var array*/protected$dates=['deleted_at'];protected$filleble=['title','type','description','images','alive'];公共函数user(){return$this->belongsTo('App\user');}公共函数orders(){return$this->hasMany('App\Order');}}
类顺序扩展模型{use SoftDeletes;/***应变异为日期的属性。**@var array*/protected$dates=['deleted_at'];protected$filleble=['comments'、'product_id'、'type'、'user_id'、'alive'、'grated'];公共函数user(){return$this->belongsTo('App\user');}}公共函数product(){return$this->belongsTo('App\product');}
你能不能变量转储($order)你看到第二个订单的输出了吗?在foreach内部,我的意思是foreach是正确的,只是你绊倒了我想X)$temp[]代码没有输出,只是在空白屏幕上,而如果我在查询中使用('products'),它会给出错误:调用模型[App\order]上未定义的关系[products]。(关系是按顺序定义的,因为它属于许多产品,而在产品中它有许多订单)…
$orders=order::where('user_id',auth()->user()->id)->orderBy('created_at','desc')->get();$temp=[];foreach($orders as$order){$temp[]=$order->product;}dd($temp);
给出以下相同的输出,只显示第一个项目,第二个项目显示null…
数组:2[▼   0=>乘积{#276▶}   1=>null]
$temp= [];
foreach($orders->all() as $order){
     $temp[] = $order->product;
}
$orders = Order::where('user_id', auth()->user()->id)
                    ->with('products')
                    ->orderBy('created_at', 'desc')
                    ->get();