PHPackages                             nullthoughts/laravel-latest-relation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nullthoughts/laravel-latest-relation

ActiveLibrary[Database &amp; ORM](/categories/database)

nullthoughts/laravel-latest-relation
====================================

Eloquent macros for querying latest HasMany relationship in Laravel

v1.0.0(6y ago)1475.5k↓36.3%4[4 PRs](https://github.com/nullthoughts/laravel-latest-relation/pulls)MITPHP

Since Feb 5Pushed 3y ago1 watchersCompare

[ Source](https://github.com/nullthoughts/laravel-latest-relation)[ Packagist](https://packagist.org/packages/nullthoughts/laravel-latest-relation)[ RSS](/packages/nullthoughts-laravel-latest-relation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (6)Used By (0)

[![Total Downloads](https://camo.githubusercontent.com/c495fce5a3b1f882cbd7faa860d205b95924d26031c5459bacdb2b5676f4aaaf/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c74686f75676874732f6c61726176656c2d6c61746573742d72656c6174696f6e2f642f746f74616c2e737667)](https://packagist.org/packages/nullthoughts/laravel-latest-relation)[![Latest Stable Version](https://camo.githubusercontent.com/8061efe97072bcc23cb4dfda0419024031d8f262c7735a5b5ff35344266caa8f/68747470733a2f2f706f7365722e707567782e6f72672f6e756c6c74686f75676874732f6c61726176656c2d6c61746573742d72656c6174696f6e2f762f737461626c652e737667)](https://packagist.org/packages/nullthoughts/laravel-latest-relation)[![Travis CI Build Status: Master](https://camo.githubusercontent.com/17790fe2494f3b4b16c4ac9ca90ca4b6ac2b6a02489178672717ad2edc1ab503/68747470733a2f2f6170692e7472617669732d63692e636f6d2f6e756c6c74686f75676874732f6c61726176656c2d6c61746573742d72656c6174696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/nullthoughts/laravel-latest-relation)

Laravel Latest Relation
=======================

[](#laravel-latest-relation)

Eloquent macros for querying the latest HasMany relationship in Laravel.

More information on the problem and solutions: [Dynamic scope on latest record in Laravel's HasMany relationships, Part 1: solving with Subqueries - nullthoughts.com](https://nullthoughts.com/development/2019/10/08/dynamic-scope-on-latest-relationship-in-laravel/)

Installation
------------

[](#installation)

Install via composer: `composer require nullthoughts/laravel-latest-relation`

Usage / Examples
----------------

[](#usage--examples)

Use the Builder methods inside a whereHas closure:

### Latest:

[](#latest)

#### whereLatestRelation($relation, $column, $operator = null, $value = null)

[](#wherelatestrelationrelation-column-operator--null-value--null)

**Query**

```
$users = User::whereLatestRelation('logins', 'device_type', '=', 'desktop');
```

**Dynamic Scope**

```
public function scopeUsingDevice($query, $device)
{
    return $query->whereLatestRelation('logins', 'device_type', $device);
}

public function scopeHavingCountry($query)
{
    return $query->whereLatestRelation('logins', 'country', '!=', 'null');
}
```

#### whereLatest($column, $value)

[](#wherelatestcolumn-value)

**Query**

```
$users = User::whereHas('logins', function ($query) {
    $query->whereLatest('device_type', 'desktop');
});
```

**Dynamic Scope**

```
public function scopeUsingDevice($query, $device)
{
    return $query->whereHas('logins', function ($query) use ($device) {
        $query->whereLatest('device_type', $device);
    });
}
```

#### latestRelation()

[](#latestrelation)

**Query**

```
$users = User::whereHas('logins', function ($query) {
    $query->latestRelation()->whereBetween(
        'created_at', [
            Carbon::now()->startOfDay(),
            Carbon::now()->endOfDay()
        ]);
});
```

**Dynamic Scope**

```
public function scopeHavingDeviceType($query)
{
    return $query->whereHas('logins', function ($query) {
        $query->latestRelation()->whereNotNull('device_type');
    });
}
```

### Earliest:

[](#earliest)

```
$users = User::whereLatestRelation('logins', 'device_type', 'desktop');

$users = User::whereHas('logins', function ($query) {
    $query->whereEarliest('device_type', 'desktop');
});

$users = User::whereHas('logins', function ($query) {
    $query->earliestRelation()->whereNotNull('device_type');
});
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 86.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2294d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ab0fb3a3f9ad10edac1e94d9ccf42330bef6d64318aba516ac3bb3befbdf4add?d=identicon)[nullthoughts](/maintainers/nullthoughts)

---

Top Contributors

[![nullthoughts](https://avatars.githubusercontent.com/u/11416468?v=4)](https://github.com/nullthoughts "nullthoughts (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![ivuorinen](https://avatars.githubusercontent.com/u/11024?v=4)](https://github.com/ivuorinen "ivuorinen (1 commits)")[![vicgonvt](https://avatars.githubusercontent.com/u/7350307?v=4)](https://github.com/vicgonvt "vicgonvt (1 commits)")

### Embed Badge

![Health badge](/badges/nullthoughts-laravel-latest-relation/health.svg)

```
[![Health](https://phpackages.com/badges/nullthoughts-laravel-latest-relation/health.svg)](https://phpackages.com/packages/nullthoughts-laravel-latest-relation)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
