Php 用滤波器进行拉维搜索

Php 用滤波器进行拉维搜索,php,laravel,laravel-5.4,laravel-eloquent,Php,Laravel,Laravel 5.4,Laravel Eloquent,下面是我的json产品详细信息及其功能 { "id": 1, "title": "Moto g4 plus", "description": "3 GB RAM | 32 GB ROM | Expandable Upto 128 GB\r\n5.5 inch Display\r\n16MP Rear Camera | 5MP Front Camera\r\n3000 mAh Battery", "price": "14999.00", "featured_image": "featured_im

下面是我的json产品详细信息及其功能

{
"id": 1,
"title": "Moto g4 plus",
"description": "3 GB RAM | 32 GB ROM | Expandable Upto 128 GB\r\n5.5 inch Display\r\n16MP Rear Camera | 5MP Front Camera\r\n3000 mAh Battery",
"price": "14999.00",
"featured_image": "featured_image/qzHWpQeSfKjZ6DOS59ROyYboJ1GCvVi6NNVfLtVV.jpeg",
"category_id": 1,
"brand_id": 4,
"created_at": "2017-07-13 14:59:53",
"updated_at": "2017-07-13 15:07:49",
"deleted_at": null,
"features": [
                {
                "id": 3,
                "name": "RAM",
                "parent_id": 1,
                "created_at": "2017-07-02 17:42:36",
                "updated_at": "2017-07-02 17:42:36",
                "default_value": "",
                "pivot": {
                "product_id": 1,
                "feature_id": 3,
                "value": "3"
                   }
                },
                {
                "id": 10,
                "name": "Expandable memory",
                "parent_id": 1,
                "created_at": "2017-07-05 15:43:29",
                "updated_at": "2017-07-05 15:43:29",
                "default_value": "",
                "pivot": {
                "product_id": 1,
                "feature_id": 10,
                "value": "32"
                    }
                },
}
现在,我希望根据功能进行过滤搜索,例如,希望手机内存为4GB,可扩展内存为64Gb。 我希望结果根据我的所有过滤器,并给我准确的结果,根据下拉选择。 下面是用户如何选择功能的下拉屏幕截图。

下面是我的产品数据库表和产品功能透视图 如上表所示,我是如何将我的产品及其功能与产品价值存储在一起的

下面是我的模型代码

public function features()
{
    return $this->belongsToMany('App\Feature')->withPivot('value');
}
Contoller.php

function searchedFeatures(Request $request)
{
    return $products = Product::with(['features' => function($q){
        $q->where('name','=','ram');
        $q->where('name','=','operating system');
    }])->get();
}

这将给我一个结果空白功能数组,我想检查功能名称及其值,例如产品有4gb的ram或产品有32Gb的可扩展内存等,并生成所有匹配产品的数组。

您需要使用发送的输入,而不是直接设置where语句

function searchedFeatures(Request $request)
{
    return $products = Product::with(['features' => function($q){
        $q->where('name','=',$request->ram);
        $q->where('os','=',$request->operating_system);
    }])->get();
}
如果它不返回任何内容,则可能与数据库中的内容不匹配。您确定输入的值正确且与数据库中的值相似吗?

使用
或where()
方法,这应该是查询中缺少的部分

return $products = Product::with(['features' => function($q){
    $q->where('name','=','ram')
      ->orWhere('os','=','operating system');
}])->get();

在尝试了这么多的东西和研发之后,我找到了通过自连接表使用DB查询的解决方案,如下所示。如果有更好的解决方案,请在这里分享

function searchedFeatures(Request $request)
{
    $ram = Feature::where('name','ram')->first();
    $processor = Feature::where('name','processor technology')->first();
    $screen = Feature::where('name','screen size')->first();
    $battery = Feature::where('name','battery capacity')->first();
    $os = Feature::where('name','operating system')->first();
    $mainCamera = Feature::where('name','main camera')->first();
    $selfieCamera = Feature::where('name','selfie camera')->first();
    $price = Feature::where('name','price')->first();
    //return $request->all();
    $products = DB::table('products AS p')
                ->select('p.id','p.title','p.price','p.featured_image','r.value AS ram','pro.value as processor','s.value AS screen','b.value AS battery','os.value AS os','mc.value AS main_camera','sc.value AS selfie_camera')
                ->leftJoin('feature_product as r','r.product_id','=','p.id')
                ->leftJoin('feature_product as pro','pro.product_id','=','p.id')
                ->leftJoin('feature_product as s','s.product_id','=','p.id')
                ->leftJoin('feature_product as b','b.product_id','=','p.id')
                ->leftJoin('feature_product as os','os.product_id','=','p.id')
                ->leftJoin('feature_product as mc','mc.product_id','=','p.id')
                ->leftJoin('feature_product as sc','sc.product_id','=','p.id')
                ->where('r.feature_id','=',$ram->id)
                ->where('pro.feature_id','=',$processor->id)
                ->where('s.feature_id','=',$screen->id)
                ->where('b.feature_id','=',$battery->id)
                ->where('os.feature_id','=',$os->id)
                ->where('mc.feature_id','=',$mainCamera->id)
                ->where('sc.feature_id','=',$selfieCamera->id)
                ->where('r.value','LIKE',isset($request->ram)? $request->ram:NULL)
                ->where('pro.value','LIKE',isset($request->processor)?$request->processor.'-core':NULL)
                ->whereBetween('s.value',isset($request->screen)?(explode(',', $request->screen)):[1,100] )
                ->whereBetween('b.value',isset($request->battery)?(explode(',', $request->battery)):[1000,5000] )
                ->where('os.value','LIKE',isset($request->os)? $request->os:NULL)
                ->whereBetween('mc.value',isset($request->main_camera)?(explode(',', $request->main_camera)):[2,50] )
                ->whereBetween('sc.value',isset($request->selfie_camera)?(explode(',', $request->selfie_camera)):[2,50] )
                ->whereBetween('p.price',isset($request->price)?(explode(',', $request->price)):[1000,80000] )
                ->get();
    $categoryPage=true;                    
    return view('product.searched',compact(['products','categoryPage']));

}

你的意思是你想使用请求值来过滤查询结果吗?@Omisakinoruwatobi我想从从json数组中过滤下拉列表中选择值,并给我结果。例如,如果我从下拉列表中选择ram 4Gb,则匹配的具有4Gb ram的产品在结果中给我,在轴值中给我类似ram的功能值。@Nilessingh,当您选择该下拉列表时,是否可以在控制器中显示您收到的请求?您应该使用
whereHas
,而不是
with
。我刚才也进去了。