我有订阅表加入的客户模型和产品模型。当我运行类似的东西
$customers = Customer::with(['products' => function($query) { $query->where('somecondition');}])->get();
如果我像以下客户一样:
foreach ($customers as $customer){
foreach($customer->products as $product) {
# do something
}
}
它通过使用动态属性加载客户的整个产品而忽略上述条件。如何解决此问题?
我的课程
class Customer extends Model{
public function products(){
return $this->belongsToMany('App\Product', 'subscriptions')->withTimestamps()
->withPivot('qty', 'start_date', 'end_date');
}
}
我在另一个函数中调用它:
$customers = $request->user()->customers()->where('id', $request['customer_id'])->whereHas(
'customerstatuses', function ($q) use ($p){
$q->where('start_date', '<' ,$p)
->where('end_date', '>' ,$p)
->where('active_yn', '=' ,'1');
})->with(['products'=> function ($query) use ($p){
$query->whereHas('productstatuses' ,function ($q) use ($p){
$q->where('start_date', '<' ,$p)
->where('end_date', '>' ,$p)
->where('active_yn', '=' ,'1');
})->wherePivot('start_date', '<', $p )
->wherePivot('end_date', '>', $p)])->get();
答案 0 :(得分:0)
你做错了..你应该这样做
factory = sessionmaker()
factory.configure(bind=engine)
session = factory()
关系条件将在$customers = Customer::with(['products' => function($query) { $query->where('somecondition');}]);
。