如何在Laravel中打印与其类别名称相关的品牌名称?

时间:2018-12-07 05:37:48

标签: php mysql laravel-5

使用laravel 5.6和mysql。我的表名称为vehicles

id  name  categoryname  brandname    model
1   juy   car           toyota       121
2   gty   van           nissan       caravan
3   bgh   car           bmw          520d
4   hyu   van           ford         max
5   nhj   car           toyota       121

现在我需要分别打印与这样的类别名称相关的每个品牌名称,

car
toyota
bmw

van
nissan
ford

我该如何在控制器中执行此操作?

2 个答案:

答案 0 :(得分:2)

尝试此代码

    $vehicles = DB::table('vehicles')
                    ->orderBy('categoryname', 'asc')
                    ->get();


    $cat = "";
    foreach($vehicles AS $vehicle) {
        if($vehicle->categoryname != $cat) {
            $cat = $vehicle->categoryname;
            echo $cat . '<br><br>';
        }

        echo $vehicle->brandname . '<br>';
    }

答案 1 :(得分:0)

ArrayList list3 = new ArrayList(list1.subList(list1.size() - list2.size(), list2.size())