dimanche 4 décembre 2016

Return queried model in Eloquent attribute

I'm using Laravel's eloquent ORM, and I was hoping to be able to return a queried laravel model as an attribute, or more ideally as an eloquent relation. This is what I'm trying to do:

class CalendarEvent extends Model
{
    protected $appends = array('conflicts');

    public function getConflictsAttribute () {
        $conflicts =  CalendarEvent::where('calendar_event_type','=',$this->calendar_event_type)
                      ->where('start','<',$this->end)
                      ->where('end','>',$this->start)
                      ->get();   
    }
}

This is an attempt to get over-lapping calendar events, which would be the conflicting events, returned as an object against each event.

This would then mean any query run would return the conflicts :

$event = CalendarEvent::where('id','=',123)->first();
$event->conflicts; // conflicting events



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2goFo1t
via IFTTT

Aucun commentaire:

Enregistrer un commentaire