Sunday, 15 September 2013

php - Laravel - restrict query through relations? -



php - Laravel - restrict query through relations? -

i've been hitting head against wall this, need bit of help. i'm using laravel 4.2.4. have 2 models, event , eventdate.

class="lang-php prettyprint-override">// event has id, name class event extends eloquent { protected $table = 'events'; protected $guarded = ['id']; public $timestamps = false; public function eventdates() { homecoming $this->hasmany('\eventdate', 'event_id', 'id'); } } // eventdate has id, event_id, start_date, end_date class eventdate extends eloquent { protected $table = 'eventdates'; protected $guarded = ['id']; public $timestamps = false; }

now i'm trying events between start , end date. according docs, should able do:

class="lang-php prettyprint-override">\event::with('eventdates')->where('start_date', '>=', '2014-01-01')->where('end_date', '<=', '2014-02-01')->get();

but throw sqlstate[42s22]: column not found: 1054 unknown column 'eventdates.start_date' in 'where clause'.

i've tried using closures (if using repository pattern) such as:

class="lang-php prettyprint-override">return $this->model->with(['eventdates' => function($query) utilize ($startdate, $enddate) { $query->where('start_date', '<=', $enddate->format('y-m-d')); $query->where('end_date', '>=', $startdate->format('y-m-d')); }]);

this feels dumb error, can't figure out. ideas?

you may seek this:

$events = \event::with(array('eventdates' => function($query){ // querying in eventdates table $query->where('start_date', '>=', '2014-01-01') ->where('end_date', '<=', '2014-02-01'); }))->get();

it's because start_date , end_date fields available in eventdates table.

php laravel laravel-4

No comments:

Post a Comment