如何从Laravel 5.6中的控制器获取数据副类别名称? [英] How to get datas vice categoryname from controller in Laravel 5.6?

查看:90
本文介绍了如何从Laravel 5.6中的控制器获取数据副类别名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用laravel 5.6和mysql.我有以下表格名称为vehicles

working with laravel 5.6 and mysql. I have following table name as 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
6   gtr   car           toyota       corolla
7   gtr   van           nissan       caravan

我正在使用以下控制器功能来显示带有品牌名称和型号名称的类别名称

i am using following controller function to display categorynames with brandnames and modelnames

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

并使用以下刀片文件显示数据

and use following blade file to show data

<?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

这显示如下数据,
汽车
丰田
121(2)
丰田
花冠(1)
宝马
520d(1)


货车
日产
carvan(1)
福特
最大(1)

this is showing data as following,
car
toyota
121(2)
toyota
corolla(1)
bmw
520d(1)


van
nissan
carvan(1)
ford
max(1)

但是现在我需要进行一些更改,这意味着首先要使用以下控制器功能来指定类别名称,

but now I need change something, that means first give categorynames as using following controller function,

$names = Vehicle::groupBy('categoryname')->select('id', 'categoryname', \DB::raw('COUNT(*) as cnt'))->get();

和刀片文件

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

,现在类别数据显示为带有超链接,如下所示,
汽车
货车

and now categorydata showing with hyperlink as following,
car
van

现在,当我单击上面的类别名称时,需要显示与vehicles表中的类别名称相关的数据.以下控制器正在针对汽车类别手动工作

now I need when click above categoryname showing data related to categoryname fron the vehicles table. following controller is working manually for car category

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

                    ->where('categoryname', 'car')
                    ->get();

                    return view('vehicles.bus')->withCar($car);

但是当我单击上面的类别名称时,我无法找到如何在控制器中自动设置每个类别名称数据的方法吗?你能给我一些解决方法吗?

but I can't find out how to automatically set each categoryname data in controller when click above categotyname? can you give me some solution here?

推荐答案

首先,这个问题不容易理解,您应该对其进行编辑并使问题更易于理解.

First, the question is not easy to understand, you should edit it and make the problem more easy to understand.

我了解的是,您需要显示在右上方的链接中单击categoryname的车辆吗?

what i understood is that you need to show vehicles that has the categoryname clicked in the link above right?

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

if it is like that, then you must change some of your code like this.

首先,您必须像这样将categoryname添加到您的网址中

first you must add the categoryname to your url like this

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

然后在您的路线中必须有类似的东西

then in your route you must have something like this

Route::method('your/route/url/{cat}','YourController@yourMethod)->name('vehicle.brand');

重要的部分是将路径参数{cat}添加到您的路径中,其余的取决于您.

the important part is to add the route param {cat} to your route, the rest is up to you.

然后在您的控制器中获得这样的参数

then in your controller you get the param like this

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

and presto, you are now filtering the query by categoryname

如果这不是您所需要的,请重新提出您的问题.

if that is not what you needed, then please reformulate your question.

这篇关于如何从Laravel 5.6中的控制器获取数据副类别名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆