我是第一次使用 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()
但是根据这个 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