如何使用Blade在foreach中做foreach

时间:2016-10-10 10:56:12

标签: php foreach laravel-5

在我的控制器中,我做了类似的事情:

<?php
class DashboardController extends Controller
{
    public function index(){
            $stock = DB::table('stock')
                ->join('quality', 'stock.qualityid', '=', 'quality.qualityid')
                ->select('stock.*', 'quality.name' )
                ->get();
            $data['stock'] = $stock;

        return view('dashboard', $data);
     }
}
?>

and in my view (blade):
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HISFA -- Dashboard</title>
</head>
<body>
@foreach ($rawmaterial as $rawmaterials)
    <p>Grondstof met naam {{ $rawmaterials->type }} heeft op dit moment een stock van {{ $rawmaterials->quantity }} octabins.</p>
@endforeach
</body>
</html>

在我的数据库中,我有一个表(库存包含字段:id,qualityid,height,quantity)和第二个表(Quality with fields:qualityid,type) 现在我想从每个不同类型的每个高度数量。

帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

<?php
class DashboardController extends Controller
{
    public function index(){
            $stock = DB::table('stock')
                ->join('quality', 'stock.qualityid', '=', 'quality.qualityid')
                ->select('stock.*', 'quality.name' )
                ->get();               

        return view('dashboard', ['rawmaterials' => $stock]);
     }
}
?>

and in my view (blade):
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HISFA -- Dashboard</title>
</head>
<body>
@foreach ($rawmaterials as $rawmaterial)
    <p>Grondstof met naam {{ $rawmaterials->type }} heeft op dit moment een stock van {{ $rawmaterials->quantity }} octabins.</p>
@endforeach
</body>
</html>