php - Get object only when relation counts result is more then 0 [laravel] -


i have problem data when relation query count more 0.

this model of customer relation

class customer extends model {     protected $table = 'customers';      public function contracts()     {         return $this->hasmany('app\contract');     } 

this model of contracts

class contract extends model {     public function customer()     {         return $this->belongsto('app\customer');     } } 

on end need customers contracts beetwen 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); 

but have customers. , looks this:

"customer 1" "customer 2" "customer 3"   *"contract 1"   *"contract 2"   *"contract 3" "customer 4"   *"contract 1"   *"contract 2"   *"contract 3"   "customer 4"   

in example don't need customer 1,2, , 5. how can eager loading , object relation on end.

enter image description here

this happen, dont need customer x on screenshot - mean, don't need customer 0 contracts query

use wherehas():

$customers = customer::with('contracts')     ->wherehas('contracts', function ($q) {         $q->where('data_end','>=','2017-07-01')           ->where('data_end','<=','2017-07-31')            ->where('typ', u');     })     ->paginate(10); 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -