Php 如何从Laravel 5.6中的控制器获取数据副类别名称?

Php 如何从Laravel 5.6中的控制器获取数据副类别名称?,php,laravel,Php,Laravel,使用Laravel5.6和mysql。我的下表名称为车辆 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

使用Laravel5.6和mysql。我的下表名称为
车辆

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
6   gtr   car           toyota       corolla
7   gtr   van           nissan       caravan
我使用以下控制器函数来显示带有品牌名和型号名的categorynames

$buss = DB::table('vehicles')
                    ->select('categoryname','brandname','modelname', DB::raw('count(*) as total'))
                    ->orderBy('categoryname', 'asc')
                    ->groupBy('modelname')
                    ->get();

                    return view('vehicles.bus')->withBuss($buss);
并使用以下刀片文件显示数据

<?php $cat = ""; ?>
@foreach($vehicles->unique('modelname') as $vehicle)
   @if($vehicle->categoryname != $cat )
            <?php $cat = $vehicle->categoryname; ?>
             {{$cat}}
            <br><br>
     @endif
     <ul>
    <li>{{$vehicle->brandname}}</li> <br>

  </ul>
@endforeach
刃锉

@foreach($names as $name)
        <a href="{{ route('vehicles.brand') }}">{{$name->categoryname}}</a> 
    @endforeach

但是,当单击categoryname上方时,我找不到如何在控制器中自动设置每个categoryname数据?你能在这里给我一些解决方案吗?

首先,这个问题不容易理解,你应该编辑它,使问题更容易理解

我的理解是,您需要显示右上方链接中单击了
类别名称的车辆

如果是这样,那么您必须像这样更改一些代码

首先,您必须像下面这样将
categoryname
添加到您的url

    @foreach($names as $name)
        <a href="{{ route('vehicles.brand',['cat' => $name->categoryname]) }}">{{$name->categoryname}}</a> 
    @endforeach
Route::method('your/route/url/{cat}','YourController@yourMethod)->name('vehicle.brand');
public function yourMethod(Request $request, $cat) {
    //finally you add the $cat variable to your query like this
    $car = DB::table('vehicles')
                ->select('categoryname','brandname','modelname', DB::raw('count(*) as total'))
                ->orderBy('categoryname', 'asc')
                ->groupBy('modelname')

                ->where('categoryname', $cat)
                ->get();

                return view('vehicles.bus')->withCar($car);
}
重要的部分是将route param
{cat}
添加到您的路由中,其余部分由您决定

然后在你的控制器中,你可以得到如下参数

    @foreach($names as $name)
        <a href="{{ route('vehicles.brand',['cat' => $name->categoryname]) }}">{{$name->categoryname}}</a> 
    @endforeach
Route::method('your/route/url/{cat}','YourController@yourMethod)->name('vehicle.brand');
public function yourMethod(Request $request, $cat) {
    //finally you add the $cat variable to your query like this
    $car = DB::table('vehicles')
                ->select('categoryname','brandname','modelname', DB::raw('count(*) as total'))
                ->orderBy('categoryname', 'asc')
                ->groupBy('modelname')

                ->where('categoryname', $cat)
                ->get();

                return view('vehicles.bus')->withCar($car);
}
现在,您可以通过
categoryname


如果这不是您需要的,请重新表述您的问题。

此处没有任何答案