2016-11-23 50 views
3

我正面臨一個問題。誰能幫我 首先,我有兩個型號如何在laravel中的多個關聯表中調用5.2

1)user.php的

enter code here 
public function listings(){ 
    return $this->hasOne('App\Listing','user_id') 
      ->with("topservices"); 
} 

現在我有上市型號

2)(Listing.php)

enter code here 
public function topservices(){ 
    return $this->hasMany('App\Sellerprofile','list_id'); 
} 

現在我有一個功能

enter code here 
public function nails(){ 
    $users = DB::table('users') 
     ->where(['service_name'=>"Seller"]) 
     ->get(); 
    $users = json_decode(json_encode($users),true); 
    foreach($users as $alluser){ 
     $ids[] = $alluser['id']; 
    } 
    if(!empty($ids)){ 
      $allData = User::with('listings')->whereIn('id',$ids)->get(); 
      $allData = json_decode(json_encode($allData),true); 
      echo "<pre>"; print_r($allData); die; ///when i print this array it gives me below output 

    } 
    $title = "Nails"; 
    return view("listings.listing",['allData'=>$allData,'title'=>$title]); 
} 

輸出:

enter code here 
Array 
(
[0] => Array 
    (
     [id] => 1 
     [title] => Mr 
     [firstname] => kunal 
     [email] => [email protected] 
     [listings] => Array 
      (
       [id] => 1 
       [user_id] => 1 
       [type] => premimum 
       [business_name] => kunal 
       [topservices] => Array 
        (
         [0] => Array 
          (
           [id] => 5 
           [service_id] => 1 
           [list_id] => 1 
           [name] => Acrylic 
           [duration] => 10mins 
           [price] => 10 
          ) 

         [1] => Array 
          (
           [id] => 6 
           [service_id] => 2 
           [list_id] => 1 
           [name] => Hair Top 
           [duration] => 30mins 
           [price] => 20 
          ) 

         [2] => Array 
          (
           [id] => 7 
           [service_id] => 3 
           [list_id] => 1 
           [name] => Skin Care Top 
           [duration] => 1hr-30mins 
           [price] => 30 
          ) 

         [3] => Array 
          (
           [id] => 8 
           [service_id] => 4 
           [list_id] => 1 
           [name] => Massage Top 
           [duration] => 20mins 
           [price] => 50 
          ) 

        ) 
      ) 

    ) 

[1] => Array 
    (
     [id] => 2 
     [title] => Mr 
     [firstname] => kunal 
     [email] => [email protected] 
     [listings] => Array 
      (
       [id] => 2 
       [user_id] => 2 
       [type] => premimum 
       [topservices] => Array 
        (
         [0] => Array 
          (
           [id] => 9 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Acrylic 
           [duration] => 30mins 
           [price] => 10 
          ) 

         [1] => Array 
          (
           [id] => 10 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Powder gel nails 
           [duration] => 45mins 
           [price] => 20 
          ) 

         [2] => Array 
          (
           [id] => 11 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Polish change 
           [duration] => 10mins 
           [price] => 25 
          ) 

         [3] => Array 
          (
           [id] => 12 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Hand design 
           [duration] => 2hrs 
           [price] => 100 
          ) 

         [4] => Array 
          (
           [id] => 13 
           [service_id] => 7 
           [list_id] => 2 
           [name] => Hair Top 
           [duration] => 30mins 
           [price] => 200 
          ) 

        ) 
      ) 

    ) 
) 

現在你上面的輸出看到有Topservices陣列我想下topservices其價格大於等於比= 10,少到30 我的預期成果是所有這些陣列: -

enter code here 
Array 
(
[0] => Array 
    (
     [id] => 1 
     [title] => Mr 
     [firstname] => kunal 
     [email] => [email protected] 
     [listings] => Array 
      (
       [id] => 1 
       [user_id] => 1 
       [type] => premimum 
       [business_name] => kunal 
       [topservices] => Array 
        (
         [0] => Array 
          (
           [id] => 5 
           [service_id] => 1 
           [list_id] => 1 
           [name] => Acrylic 
           [duration] => 10mins 
           [price] => 10 
          ) 

         [1] => Array 
          (
           [id] => 6 
           [service_id] => 2 
           [list_id] => 1 
           [name] => Hair Top 
           [duration] => 30mins 
           [price] => 20 
          ) 
        ) 
      ) 

    ) 

[1] => Array 
    (
     [id] => 2 
     [title] => Mr 
     [firstname] => kunal 
     [email] => [email protected] 
     [listings] => Array 
      (
       [id] => 2 
       [user_id] => 2 
       [type] => premimum 
       [topservices] => Array 
        (
         [0] => Array 
          (
           [id] => 9 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Acrylic 
           [duration] => 30mins 
           [price] => 10 
          ) 

         [1] => Array 
          (
           [id] => 10 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Powder gel nails 
           [duration] => 45mins 
           [price] => 20 
          ) 

         [2] => Array 
          (
           [id] => 11 
           [service_id] => 6 
           [list_id] => 2 
           [name] => Polish change 
           [duration] => 10mins 
           [price] => 25 
          ) 

        ) 
      ) 

    ) 
) 

任何人都可以幫助我。在此先感謝我所做的指甲功能

回答

0

在這裏,我找到了我自己的解決方案。

enter code here 
$allData = User::with(['listings.topservices'=>function($query){ 
      $query->whereBetween('price',[10,350]); 
      }]) 
      ->whereIn('id',$ids)->get(); 
0

您可以更改您的方法以包括條件。
您可以使用whereBetween方法:

public function listings($lowerPriceLimit = null,$upperPriceLimit = null){ 
    $query = $this->hasOne('App\Listing','user_id'); 
    if($lowerPriceLimit != null && $upperPriceLimit != null) 
     $query = $query->with(["topservices"=>function($q) use($lowerPriceLimit,$upperPriceLimit){ 
          $q->whereBetween('price',[$lowerPriceLimit,$upperPriceLimit]); 
         }]); 
    else 
     $query->with("topservices"); 
    return $query; 
} 
+0

不工作@jaysingkar它給我的錯誤(爆炸()預計參數2爲字符串,對象給出):( – kunal

+0

使用價格whereBetween價格列不在列表表格是在topservices希望你能理解它給了我錯誤(在'where子句'中的未知列'價格'(SQL:select * from'listing',其中'(1,2)'中的列表'''用戶標識'和10到30之間的'價格')) – kunal

相關問題