收藏->;put()在Laravel 5.2中不起作用


Collection->put() not working in Laravel 5.2

我是第一次使用Laravel。我有一个场景,我有一张产品表,其中包含产品(瓦楞纸箱)的基本细节,如长度、宽度、高度等。产品的其他一些细节是使用函数中的基本细节计算的。

我在控制器中的代码如下:

控制器:

$products = DB::table('master_products')
            ->join('part_types', 'master_products.part_type_id', '=', 'part_types.id')
            ->join('box_types', 'master_products.box_type_id', '=', 'box_types.id')
            ->select('master_products.*', 'part_types.part_type', 'box_types.box_type')
            ->get();
    /* Calculate Specs and add them to the collection */
    foreach ($products as $product) {
        $rollSize = $this->calcRollSize($product->height, $product->breadth, $product->ply, $product->part_type_id, $product->box_type_id);
        $products->put('roll_size', $rollSize); 
    }
return view('/layouts/masters/products-master', ['products' => $products]);

视图:

<table class="table table-bordered table-condensed table-hover">
            <thead>
                <tr>
                    <th>Sl.No.</th>
                    <th>Product Code</th>
                    <th>Part Type</th>
                    <th>Box Type</th>
                    <th>Length</th>
                    <th>Breadth</th>
                    <th>Height</th>
                    <th>Ply</th>
                    <th>Roll Size</th>
                    <th>A-Base</th>
                    <th>A-Flute</th>
                    <th>B-Base</th>
                    <th>B-Flute</th>
                    <th>Top</th>
                </tr>
            </thead>
            <tbody>                    
                @foreach($prod_specs as $prod_spec)
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $prod_spec->product_code; ?></td>
                    <td><?php echo $prod_spec->part_type; ?></td>
                    <td><?php echo $prod_spec->box_type; ?></td>
                    <td><?php echo $prod_spec->length; ?></td>
                    <td><?php echo $prod_spec->breadth; ?></td>
                    <td><?php echo $prod_spec->height; ?></td>
                    <td><?php echo $prod_spec->ply; ?></td>
                    <td><?php echo $prod_spec->roll_size; ?></td>
                    <td><?php echo $prod_spec->gsm_a_base; ?></td>
                    <td><?php echo $prod_spec->gsm_a_flute; ?></td>
                    <td><?php echo $prod_spec->gsm_b_base; ?></td>
                    <td><?php echo $prod_spec->gsm_b_flute; ?></td>
                    <td><?php echo $prod_spec->gsm_top; ?></td>
                </tr>
                <?php $i++; ?>
                @endforeach
            </tbody>
        </table>

我得到了这个异常:对非对象上的成员函数put()的调用

但根据这个stackoverflow问题的公认答案,它应该是有效的。我在这里错过了什么?

更新:

尝试过这个

foreach ($products as $product) {
    $rollSize = $this->calcRollSize($product->height, $product->breadth, $product->ply, $product->part_type_id, $product->box_type_id);
    $product['roll_size'] = $rollSize; 
}

出现错误Cannot use object of type stdClass as array

您将从查询生成器中获得一个数组。Laravel collect()助手是您的解决方案:

$products = collect(DB::table('master_products')
            ->join('part_types', 'master_products.part_type_id', '=', 'part_types.id')
            ->join('box_types', 'master_products.box_type_id', '=', 'box_types.id')
            ->select('master_products.*', 'part_types.part_type', 'box_types.box_type')
            ->get());

更多信息

查询生成器在调用get时不返回集合,而是返回一个数组:

与原始查询一样,get方法返回一个结果数组,其中每个结果都是PHP StdClass对象的一个实例。

如果您想要Eloquent集合,则需要使用Eloquet模型或使用@Martin建议的collect()辅助函数。

put()方法设置集合中给定的keyvalue

您正在尝试将数组对象保存在$rollSize中。

若要将rollSize作为数组存储在数据库中,则在模型中使roll_size序列化对象。