Laravel通过过滤器搜索

时间:2017-08-07 11:17:43

标签: 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_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"
                    }
                },
}

现在我希望根据功能进行过滤搜索,例如想要手机的RAM为4GB,可扩展内存为64Gb。 我想按照我的所有过滤器的结果,并根据下拉选择给我准确的结果。 下方截图用于下拉用户选择功能的方式。 screenshot

下面是产品功能的产品和数据透视表的数据库表 Product product_feature 如上表所示,我如何将产品及其功能存储在产品价值中。

以下是我的型号代码

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等,并产生所有匹配产品的数组。

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

尝试了这么多东西之后,我通过自联接表找到了使用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']));

}

答案 2 :(得分:-1)

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

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

如果它没有返回它可能与数据库中的不匹配。您确定输入的值是否正确且类似于数据库中的值?