{"id":40698939,"url":"https://github.com/nullthoughts/laravel-latest-relation","last_synced_at":"2026-01-21T11:41:45.712Z","repository":{"id":49097436,"uuid":"213065677","full_name":"nullthoughts/laravel-latest-relation","owner":"nullthoughts","description":"Eloquent macros for querying latest HasMany relationship in Laravel","archived":false,"fork":false,"pushed_at":"2023-02-02T07:50:23.000Z","size":65,"stargazers_count":14,"open_issues_count":5,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-25T05:34:26.481Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nullthoughts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-05T20:18:38.000Z","updated_at":"2022-11-27T21:46:07.000Z","dependencies_parsed_at":"2023-02-17T16:20:25.581Z","dependency_job_id":null,"html_url":"https://github.com/nullthoughts/laravel-latest-relation","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nullthoughts/laravel-latest-relation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nullthoughts%2Flaravel-latest-relation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nullthoughts%2Flaravel-latest-relation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nullthoughts%2Flaravel-latest-relation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nullthoughts%2Flaravel-latest-relation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nullthoughts","download_url":"https://codeload.github.com/nullthoughts/laravel-latest-relation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nullthoughts%2Flaravel-latest-relation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28632774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T04:47:28.174Z","status":"ssl_error","status_checked_at":"2026-01-21T04:47:22.943Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-21T11:41:45.639Z","updated_at":"2026-01-21T11:41:45.701Z","avatar_url":"https://github.com/nullthoughts.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://packagist.org/packages/nullthoughts/laravel-latest-relation\" target=\"_blank\"\u003e\u003cimg src=\"https://poser.pugx.org/nullthoughts/laravel-latest-relation/d/total.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/nullthoughts/laravel-latest-relation\" target=\"_blank\"\u003e\u003cimg src=\"https://poser.pugx.org/nullthoughts/laravel-latest-relation/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.com/nullthoughts/laravel-latest-relation\"\u003e\u003cimg src=\"https://api.travis-ci.com/nullthoughts/laravel-latest-relation.svg?branch=master\" alt=\"Travis CI Build Status: Master\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\n# Laravel Latest Relation\nEloquent macros for querying the latest HasMany relationship in Laravel. \n\nMore 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/)\n\n## Installation\nInstall via composer:\n`composer require nullthoughts/laravel-latest-relation`\n\n## Usage / Examples\nUse the Builder methods inside a whereHas closure:\n\n### Latest:\n\n#### whereLatestRelation($relation, $column, $operator = null, $value = null)\n**Query**\n```php\n$users = User::whereLatestRelation('logins', 'device_type', '=', 'desktop');\n```\n\n**Dynamic Scope**\n```php\npublic function scopeUsingDevice($query, $device)\n{\n    return $query-\u003ewhereLatestRelation('logins', 'device_type', $device);\n}\n\npublic function scopeHavingCountry($query)\n{\n    return $query-\u003ewhereLatestRelation('logins', 'country', '!=', 'null');\n}\n```\n\n#### whereLatest($column, $value)\n**Query**\n```php\n$users = User::whereHas('logins', function ($query) {\n    $query-\u003ewhereLatest('device_type', 'desktop');\n});\n```\n\n**Dynamic Scope**\n```php\npublic function scopeUsingDevice($query, $device)\n{\n    return $query-\u003ewhereHas('logins', function ($query) use ($device) {\n        $query-\u003ewhereLatest('device_type', $device);\n    });\n}\n```\n\n#### latestRelation()\n**Query**\n```php\n$users = User::whereHas('logins', function ($query) {\n    $query-\u003elatestRelation()-\u003ewhereBetween(\n        'created_at', [\n            Carbon::now()-\u003estartOfDay(),\n            Carbon::now()-\u003eendOfDay()\n        ]);\n});\n```\n\n**Dynamic Scope**\n```php\npublic function scopeHavingDeviceType($query)\n{\n    return $query-\u003ewhereHas('logins', function ($query) {\n        $query-\u003elatestRelation()-\u003ewhereNotNull('device_type');\n    });\n}\n```\n\n### Earliest:\n\n```php\n$users = User::whereLatestRelation('logins', 'device_type', 'desktop');\n\n$users = User::whereHas('logins', function ($query) {\n    $query-\u003ewhereEarliest('device_type', 'desktop');\n});\n\n$users = User::whereHas('logins', function ($query) {\n    $query-\u003eearliestRelation()-\u003ewhereNotNull('device_type');\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnullthoughts%2Flaravel-latest-relation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnullthoughts%2Flaravel-latest-relation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnullthoughts%2Flaravel-latest-relation/lists"}