2017-07-27 23 views
0

我有一個問題,獲取數據,只有當關系查詢次數超過0.1找對象,只有當關系的結果是存在[laravel]

這是我與關係客戶的模型

class Customer extends Model 
{ 
    protected $table = 'customers'; 

    public function contracts() 
    { 
     return $this->hasMany('App\Contract'); 
    } 

這是我合同

class Contract extends Model 
{ 
    public function customer() 
    { 
     return $this->belongsTo('App\Customer'); 
    } 
} 

在最終的模式,我只需要客戶他們是誰ç ontracts beetwen some date

$customers = Customer::with(['contracts' => function($query) 
    { 
     $query->where('data_end','>=','2017-07-01') 
       ->where('data_end','<=','2017-07-31') 
       ->where('typ','=','U') ; 
    } 
    ])->paginate(10); 

但我有所有的客戶。它看起來像這樣:

"Customer 1" 
"Customer 2" 
"Customer 3" 
    *"Contract 1" 
    *"Contract 2" 
    *"Contract 3" 
"Customer 4" 
    *"Contract 1" 
    *"Contract 2" 
    *"Contract 3" 
"Customer 4" 

在這個例子中我不需要客戶1,2和5。我如何與預先加載和對象與最終關係做到這一點。

enter image description here

這是發生的,我不需要客戶提供的截圖X - 我的意思是,我不需要客戶提供從0合同,即查詢

- 對象由DD() - - 此查詢 2客戶,1日有2個合同

末,第二次有0合同

LengthAwarePaginator {#217 ▼ 
    #total: 75000 
    #lastPage: 37500 
    #items: Collection {#213 ▼ 
    #items: array:2 [▼ 
     0 => Customer {#220 ▼ 
     #table: "customers" 
     #connection: null 
     #primaryKey: "id" 
     #keyType: "int" 
     +incrementing: true 
     #with: [] 
     #perPage: 15 
     +exists: true 
     +wasRecentlyCreated: false 
     #attributes: array:5 [▼ 
      "id" => 1 
      "customer_number" => "46071600600" 
      "name" => "Nikodem Zalewski" 
      "customer_contact" => "507614445" 
      "customer_type" => "P" 
     ] 
     #original: array:5 [▶] 
     #casts: [] 
     #dates: [] 
     #dateFormat: null 
     #appends: [] 
     #events: [] 
     #observables: [] 
     #relations: array:1 [▼ 
      "contracts" => Collection {#224 ▼ 
      #items: array:2 [▼ 
       0 => Contract {#227 ▼ 
       #connection: null 
       #table: null 
       #primaryKey: "id" 
       #keyType: "int" 
       +incrementing: true 
       #with: [] 
       #perPage: 15 
       +exists: true 
       +wasRecentlyCreated: false 
       #attributes: array:10 [▶] 
       #original: array:10 [▶] 
       #casts: [] 
       #dates: [] 
       #dateFormat: null 
       #appends: [] 
       #events: [] 
       #observables: [] 
       #relations: [] 
       #touches: [] 
       +timestamps: true 
       #hidden: [] 
       #visible: [] 
       #fillable: [] 
       #guarded: array:1 [▶] 
       } 
       1 => Contract {#229 ▶} 
      ] 
      } 
     ] 
     #touches: [] 
     +timestamps: true 
     #hidden: [] 
     #visible: [] 
     #fillable: [] 
     #guarded: array:1 [▶] 
     } 
     1 => Customer {#221 ▼ 
     #table: "customers" 
     #connection: null 
     #primaryKey: "id" 
     #keyType: "int" 
     +incrementing: true 
     #with: [] 
     #perPage: 15 
     +exists: true 
     +wasRecentlyCreated: false 
     #attributes: array:5 [▶] 
     #original: array:5 [▼ 
      "id" => 2 
      "customer_number" => "81050371854" 
      "name" => "Adam Wróbel" 
      "customer_contact" => "560047958" 
      "customer_type" => "P" 
     ] 
     #casts: [] 
     #dates: [] 
     #dateFormat: null 
     #appends: [] 
     #events: [] 
     #observables: [] 
     #relations: array:1 [▼ 
      "contracts" => Collection {#222 ▼ 
      #items: [] 
      } 
     ] 
     #touches: [] 
     +timestamps: true 
     #hidden: [] 
     #visible: [] 
     #fillable: [] 
     #guarded: array:1 [▶] 
     } 
    ] 
    } 
    #perPage: 2 
    #currentPage: 1 
    #path: "*" 
    #query: [] 
    #fragment: null 
    #pageName: "page" 
} 

回答

0

你幾乎有w^ith你的代碼。

不指定您正在使用的Laravel的版本,但你應該在whereHas尋找(這存在於Laravel因爲至少5.0)

https://laravel.com/docs/5.0/eloquent#querying-relations(V5.0)

訪問模型的記錄時,您可能希望根據關係的存在限制結果。例如,您希望獲取至少有一條評論的所有博客文章。要做到這一點,你可以使用有方法:

如果你需要更多的權力,你可以使用whereHas和orWhereHas方法把「其中」您有查詢條件:

請嘗試以下碼。

$customers = Customer::with('contracts')->whereHas('contracts', function($query) 
{ 
    $query->where('data_end','>=','2017-07-01') 
      ->where('data_end','<=','2017-07-31') 
      ->where('typ','=','U') ; 
} 
)->paginate(10); 

這將與客戶一起加載合同對象,但只返回具有與查詢匹配的合同的客戶。