Laravel 按过滤器搜索

Laravel search by filter

以下是我的 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"
                    }
                },
}

现在我想根据功能进行过滤搜索,例如想要 phone 具有 4GB RAM 和 64Gb 可扩展内存。 我想要根据我的所有过滤器得到结果,并根据下拉菜单 select 给我准确的结果。 下面的屏幕截图显示了用户 select 的功能。

下面是我的产品数据库 table 和产品功能的数据透视表 如上所述 table 我如何存储我的产品及其具有产品价值的功能。

下面是我的模型代码

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();
}

如果它没有返回任何内容,则可能是它与数据库中的内容不匹配。您确定输入的值正确且与数据库中的值相似吗?

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

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

在尝试了很多事情和研发之后,我通过自我加入 table 找到了使用数据库查询的解决方案,如下所示。如果有人有更好的解决方案,请在这里分享。

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']));

}