I have two tables trails and markers (one trail can have many markers). I would like to return data from both using a custom method in the trails table.
Something like this:
public function RetrieveMarker(){
return Marker::where("relation_id","=", $this->id);
}
but how do I call this method from a controller?
I would like to return a json object of several trails and their related markers like this:
[{id: 1, trail: a trail, markers: [{id: a marker}, id: another marker],}
{id: 2, trail: trail 2, markers: [{id: yam}, id: yaam]}]
and would like to do something like this:
return $this->trail
->orderBy('title', 'ASC')
->where('region_id', $region_id)
->RetriveMarkers()
->get();
but I am getting an illuminate/bad/query exception.
I have tried a couple of variations using scope but think that this isn't that difficult, I just don't know the way!
BTW, I am using Laravel 4, there isn't a direct relationship between trails and markers because markers is polymorphically related to several tables and I don't want to use the "$this->belongsTo" style as a custom method is definitely needed here.
Thanks in advance.