php - laravel - add conditions to relationships -


i have 2 models "channels" , "schedules" relationship 1 channel has many schedules (one-to-many). want channels containing schedules today , schedules need sort asc date limit 10 schedules per channel.

i wrote following code returns wrong results sometimes. checked , found laravel generating sql query limiting schedule results channels. i'm using eloquent. know how this?... thanks.

$channels = $this->channels->wherehas('tvschedules', function($q)                 {                     $q->where('day', date('y-m-d', strtotime(carbon::now()->todatestring())))                         ->where('end_time', '>=', carbon::now()->totimestring());                  })->with(['tvschedules' => function($q){                     $q->where('day', date('y-m-d', strtotime(carbon::now()->todatestring())))                         ->where('end_time', '>=', carbon::now()->totimestring())                         ->with('tvscheduleimages')                         ->take(10)                     ;                 }]); 

sql:  select * `trn_tv_schedule` `trn_tv_schedule`.`channel_id` in (1, 2) , `day` = '2015-08-31' , `end_time` >= '06:11:34' limit 10 

try using eloquent relationships in models like

// channel model public function schedules() {     return $this->hasmany('schedule'); } // schedule model public function channel() {     return $this->belongsto('channel'); } 

if using laravel 5.0 have @ - eloquent relationships


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -