Laravel Tutorials

Simplify HasManyThrough Relationships with Laravel's CanBeOneOfMany Support

Published
Simplify HasManyThrough Relationships with Laravel's CanBeOneOfMany Support image

Laravel enhances its Eloquent ORM by adding CanBeOneOfMany support for HasManyThrough relationships. This feature allows developers to elegantly retrieve a single model from a HasManyThrough relationship using clean, expressive syntax for the latest, oldest, or custom-filtered records.

Eloquent already provided powerful tools for working with complex relationships. With the addition of CanBeOneOfMany support, you can now retrieve a singular model from a HasManyThrough relationship using various criteria:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class Customer extends Model
{
    /**
     * Get the most recent order for the customer.
     */
    public function latestOrder(): HasOneThrough
    {
        return $this->orders()->one()->latestOfMany();
    }
}

This feature particularly shines when accessing specific records from larger relationship sets:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Department extends Model
{
    /**
     * Get all tasks for the department.
     */
    public function tasks(): HasManyThrough
    {
        return $this->hasManyThrough(
            Task::class, 
            Employee::class,
            'department_id',
            'employee_id'
        );
    }
    
    /**
     * Get the most recently created task for the department.
     */
    public function latestTask(): HasOneThrough
    {
        return $this->tasks()->one()->latestOfMany();
    }
    
    /**
     * Get the first task created for the department.
     */
    public function firstTask(): HasOneThrough
    {
        return $this->tasks()->one()->oldestOfMany();
    }
    
    /**
     * Get the highest priority task for the department.
     */
    public function criticalTask(): HasOneThrough
    {
        return $this->tasks()->one()->ofMany('priority', 'max');
    }
    
    /**
     * Get the most time-consuming task.
     */
    public function longestTask(): HasOneThrough
    {
        return $this->tasks()->one()->ofMany([
            'estimated_hours' => 'max',
        ]);
    }
}

With these relationship methods defined, accessing specific records becomes straightforward:

$department = Department::find(1);

// Get the most recent task
$latestTask = $department->latestTask;

// Get the first task ever created
$firstTask = $department->firstTask;

// Get the highest priority task
$criticalTask = $department->criticalTask;

// Get the task with the longest estimated hours
$longestTask = $department->longestTask;

These new relationship methods enable more expressive and maintainable code that clearly communicates model relationships, making complex data access patterns simpler and more intuitive.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article
Real-Time Inertia Props With Laravel Reverb image

Real-Time Inertia Props With Laravel Reverb

Read article
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article