iPriceVentures / ocapiplugin

iPrice RESTful API generator plugin for October CMS
MIT License
5 stars 5 forks source link

Single record in Eager loaded relation #8

Open bkrajendra opened 5 years ago

bkrajendra commented 5 years ago

Im currently trying to eager load a relation on main table (Model). By default it eager loads all records on relation. My child relation has many record and I dont want all record to be loaded. Infact i just want to load single last record (latest) record from this relation to be loaded.

is this possible with eager loading.? See my post at SO

bkrajendra commented 5 years ago

Got it resolved by: in my model I added a custom relation:

public function latestDeviceLog()
{
    return $this->hasOne('Myhome\Myplugin\Models\Devicelog')->latest();
}

Now I can access with:

Device::with('latestDeviceLog')->get();