In this laravel eloquent we will discuss how to get single data record in hasMany() or one to many relationship. How to use conditions for getting single record in hasMany relationship. In simple words we will discuss these methods on hasMany relationship in model latestOfMany(), oldestOfMany() and ofMany()
Laravel hasMany() relationship use these methods
- latestOfMany()
- oldestOfMany()
- ofMany()
Laravel hasMany() relationship latestOfMany() method
Laravel hasMany() relationship get many rows from relation table but some time we want to get a single row from these related records also called has one of many relationship then use some methods. One of them is latestOfMany() method. latestOfMany() method use when we get latest or new record which is related to main model. In our example if we get last salary of employee from salarys table with main model Employee then use latestOfMany() method with hasMany() method.
Employee modelpublic function latestSalary() { return $this->hasOne(Salary::class)->latestOfMany(); }
Laravel hasMany() relationship oldestOfMany() method
oldestOfMany() method work same as latestOfMany() method. Difference between them oldestOfMany() method get oldest data which are related to main model. these methods find old or new data according to primary key which must sortable. Example of oldestOfMany method
Employee modelpublic function latestSalary() { return $this->hasOne(Salary::class)->latestOfMany(); }
Laravel hasMany() relationship ofMany() method
Laravel oldestOfMany() and latestOfMany() method work according to primary column sorting value but if we want sort hasMany() data by different column use ofMany() method where pass first attribute column name and second is min or max value. these are get related model selected column minmum or maximum values. For example we want get maximum or minimum salary of a employee.
Employee modelpublic function largestSalary() { return $this->hasOne(Salary::class)->ofMany('amount', 'max'); } public function lowestSalary() { return $this->hasOne(Salary::class)->ofMany('amount', 'min'); }
You read this tutorial on advanced web tutorial by anmol sharma. Here we provide laravel beginners to advanced tutorial.
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- part 1 laravel hasOne() relationship
- part 2 laravel one to many relationship
- part 3 Has One Of Many relationship
- part 4 laravel hasOneThrough relationship
- part 5 laravel hasManyThrow() relationship
- part 6 laravel many to many relationship
- part 7 one to one polymorphic relationship
- part 8 laravel one to many polymorphic relationship
- part 9 many-to-many polymorphic relationship
- part 10 laravel eloquent has vs whereHas method
- part 11 laravel whereDoesntHave and whereDoesntHave
- part 12 laravel withCount() method
- part 13 eager vs lazy loading