【问题标题】:Collection->put() not working in Laravel 5.2Collection->put() 在 Laravel 5.2 中不起作用
【发布时间】:2023-03-18 14:55:01
【问题描述】:

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

我在 Controller 中的代码如下所示:

控制器:

$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()

但根据this stackoverflow question 接受的答案,它应该可以工作。我在这里错过了什么?

更新:

试过了

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

【问题讨论】:

    标签: php laravel-5.2 laravel-collection


    【解决方案1】:

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

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

    如果您想将rollSize 作为数组存储在数据库中,请在模型中创建roll_size 序列化对象。

    【讨论】:

      【解决方案2】:

      Query Builder在调用get时不返回集合,而是返回一个数组:

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

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

      【讨论】:

      • 哦,好吧。我的错。那么在这种情况下,如何将roll_size 添加到$products 数组中??
      • 与向任何其他数组添加内容相同:$products['roll_size'] = $rollSize
      • dd($product) 在您的循环中并使用输出更新您的问题。另请注意,在此循环运行后,roll_size 将只有 一个 索引。如果您需要一系列卷筒尺寸,您可能需要重新考虑您的设计。
      • 错误描述有'products' =&gt; array(object(stdClass), 'roll_size' =&gt; '1671'。看起来我正在向对象添加一个数组。 var_dump($products) 说它是一个对象
      • 您说得对,杰夫先生。我想在我看来访问$products-&gt;roll_size..请建议我该怎么做
      【解决方案3】:

      您从 Query Builder 获得了一个数组。 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());
      

      More info

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-06
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 2017-05-22
        • 1970-01-01
        • 2017-08-08
        • 2016-08-17
        相关资源
        最近更新 更多