Php 仅当关系结果存在时获取对象[laravel]
只有当关系查询计数大于0时,我才能获取数据 这是我的客户关系模型Php 仅当关系结果存在时获取对象[laravel],php,laravel,orm,relation,eager,Php,Laravel,Orm,Relation,Eager,只有当关系查询计数大于0时,我才能获取数据 这是我的客户关系模型 class Customer extends Model { protected $table = 'customers'; public function contracts() { return $this->hasMany('App\Contract'); } 这是我的合同模型 class Contract extends Model { public fun
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');
}
}
最后,我只需要他们在某个日期签订合同的客户
$customers = Customer::with(['contracts' => function($query)
{
$query->where('data_end','>=','2017-07-01')
->where('data_end','<=','2017-07-31')
->where('typ','=','U') ;
}
])->paginate(10);
在本例中,我不需要客户1、2和5。我怎样才能做到这一点与渴望加载和对象的关系上结束
这种情况发生了,我不需要屏幕截图上有X的客户-我的意思是,我不需要从何处查询有0个合同的客户
--来自dd()的对象--
此查询结束
2个客户,第一个有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"
}
你的代码就快到了 您不需要指定您使用的是哪个版本的Laravel,但您应该查看它的位置(至少从5.0开始,Laravel中就存在这种情况) (v5.0) 访问模型的记录时,您可能希望根据关系的存在限制结果。例如,您希望提取至少有一条评论的所有博客帖子。为此,您可以使用has方法: 如果您需要更多功能,您可以使用whereHas和orWhereHas方法在has查询中设置“where”条件: 请尝试以下代码
$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);
$customers=Customer::with('contracts')->whereHas('contracts',function($query)
{
$query->where('data_end','>=','2017-07-01')
->其中('data_end','
$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);