2017-07-27 63 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); 

这将与客户一起加载合同对象,但只返回具有与查询匹配的合同的客户。