New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

ankurk91/laravel-eloquent-relationships

Add missing eloquent relationships to Laravel php framework.
557,390 58
Install
composer require ankurk91/laravel-eloquent-relationships
Latest Version:2.4.0
PHP:^8.3
Laravel:11 · 12 · 13
Older releases:5 – 10
License:MIT
Last Updated:Nov 9, 2026
Links: GitHub  ·  Packagist
Maintainer: ankurk91

Missing Eloquent Relationships For Laravel

Packagist GitHub tag License Downloads tests codecov

This package adds some missing relationships to Eloquent in Laravel.

Requirements

Package Version
PHP 8.3+
Laravel 12, 13

Installation

You can install the package via composer:

composer require ankurk91/laravel-eloquent-relationships

Usage

BelongsToOne

The BelongsToOne relation is almost identical to the standard BelongsToMany relation, except it returns a single model instead of a Collection of models, and null if there is no related model in the database (BelongsToMany returns an empty Collection in this case). Example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\HasBelongsToOne;
use Ankurk91\Eloquent\Relations\BelongsToOne;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Restaurant extends Model
{
    use HasBelongsToOne;

    /**
     * Each restaurant has only one operator.
     */
    public function operator(): BelongsToOne
    {
        return $this->belongsToOne(User::class)
            ->wherePivot('is_operator', true);
            //->withDefault();
    }

    /**
     * Get all employees including the operator.
     */
    public function employees(): BelongsToMany
    {
        return $this->belongsToMany(User::class)
            ->withPivot('is_operator');
    }
}

Now you can access the relationship like:

<?php

// eager loading
$restaurant = Restaurant::with('operator')->first();
dump($restaurant->operator);
// lazy loading
$restaurant->load('operator');
// load nested relation
$restaurant->load('operator.profile');
// Perform operations
$restaurant->operator()->update([
    'name' => 'Taylor',
]);

MorphToOne

The MorphToOne relation is almost identical to the standard MorphToMany relation, except it returns a single model instead of a Collection of models, and null if there is no related model in the database (MorphToMany returns an empty Collection in this case). Example:

<?php

namespace App\Models;

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

class Image extends Model
{
    public function posts(): MorphToMany
    {
        return $this->morphedByMany(Post::class, 'imageable');
    }

    public function videos(): MorphToMany
    {
        return $this->morphedByMany(Video::class, 'imageable');
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\HasMorphToOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Ankurk91\Eloquent\Relations\MorphToOne;

class Post extends Model
{
    use HasMorphToOne;

    /**
     * Each post may have one featured image.
     */
    public function featuredImage(): MorphToOne
    {
        return $this->morphToOne(Image::class, 'imageable')
            ->wherePivot('featured', 1);
            //->withDefault();
    }

    /**
     * Get all images including the featured.
     */
    public function images(): MorphToMany
    {
        return $this->morphToMany(Image::class, 'imageable')
            ->withPivot('featured');
    }
}

Now you can access the relationship like:

<?php

// eager loading
$post = Post::with('featuredImage')->first();
dump($post->featuredImage);
// lazy loading
$post->load('featuredImage');

The inverse relation is available via morphedByOne():

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\HasMorphToOne;
use Ankurk91\Eloquent\Relations\MorphToOne;

class Image extends Model
{
    use HasMorphToOne;

    /**
     * Get the post this image is featured on.
     */
    public function featuredOnPost(): MorphToOne
    {
        return $this->morphedByOne(Post::class, 'imageable')
            ->wherePivot('featured', 1);
    }
}

Testing

composer test

Security

If you discover any security issues, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.

Attribution

License

The MIT License.

Related Packages

sofa/hookable

Laravel Eloquent hooks system.

2,057,062 64
volosyuk/simple-eloquent

Adaptation of eloquent for retrieving its attributes

119,363 55
gzero/eloquent-tree

Eloquent Tree is a tree model for Laravel Eloquent ORM.

59,893 131
martinbean/laravel-sluggable-trait

A trait you can apply to Eloquent models to have slugs automatically generated o...

49,244 24
isswp101/elasticsearch-eloquent

Elasticsearch functionality like Laravel Eloquent models.

26,254 112

Version History

Version Released PHP Laravel License
2.4.0 ^8.3 ^11 || ^12 || ^13.0 MIT
2.3.0 ^8.3 ^11 || ^12 MIT
2.2.2 ^8.2 ^10 || ^11 || ^12.0 MIT
2.2.0 ^8.2 ^10 || ^11 || ^12.0 MIT
2.1.0 ^8.1 ^9.34 || ^10 || ^11 MIT