2017-08-07 148 views
0

以下是我的json產品詳細信息及其功能。Laravel通過篩選條件搜索

{ 
"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的RAM和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(); 
} 

這會給我造成黑屏功能陣列,我想檢查功能名稱及其如產品價值的RAM 4GB或產品具有可擴展內存32Gb等,並導致所有匹配產品陣列。

+0

你的意思是你想使用請求值來過濾查詢結果嗎? –

+0

@OmisakinOluwatobi我想從json數組中過濾下拉列表中選定的值,並給出結果,例如,如果我從下拉列表中選擇ram 4Gb,然後匹配的產品誰有4gb ram給我的結果和樞軸值是像ram功能的值。 –

+0

@Nileshsingh,當你選擇下拉菜單時,你能否在控制器中顯示你收到的請求? –

回答

0

的缺失部分嘗試這麼多的事和R & d我找到解決方案使用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'])); 

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

如果它沒有返回任何數據,它可能與數據庫中的數據不匹配。您確定輸入的值是正確的嗎?與數據庫中的數據類似嗎?

0

使用orWhere()方法,這應該是您查詢

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