ankurk91 / laravel-eloquent-relationships by ankurk91

Add missing eloquent relationships to Laravel php framework.
331,972
57
3
Package Data
Maintainer Username: ankurk91
Package Create Date: 2019-01-05
Package Last Update: 2024-02-17
Home Page: https://packagist.org/packages/ankurk91/laravel-eloquent-relationships
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:24:49
Package Statistics
Total Downloads: 331,972
Monthly Downloads: 9,004
Daily Downloads: 333
Total Stars: 57
Total Watchers: 3
Total Forks: 6
Total Open Issues: 0

Laravel Eloquent Relationships

Packagist GitHub tag License Downloads Build Status codecov

This package adds some missing relationships to eloquent in Laravel v5.7+

Installation

You can install the package via composer:

composer require ankurk91/laravel-eloquent-relationships

Usage

BelongsToOne

BelongsToOne relation is almost identical to standard BelongsToMany except it returns one model instead of Collection of models and null if there is no related model in DB (BelongsToMany returns empty Collection in this case). Example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ankurk91\Eloquent\BelongsToOne;

class Restaurant extends Model
{
    use BelongsToOne;
    
    /**
     * Each restaurant has only one operator.
     * 
     * @return \Ankurk91\Eloquent\Relations\BelongsToOne
     */
    public function operator()
    {
        return $this->belongsToOne(User::class)          
            ->wherePivot('is_operator', true);
            //->withDefault();
    }

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

Now you can access the relationship like:

// eager loading
$restaurant = Restaurant::with('operator')->first();
dump($restaurant->operator);
// lazy loading
$restaurant->load('operator');
// load nested relation
$restaurant->load('operator.profile');

MorphToOne

MorphToOne relation is almost identical to standard MorphToMany except it returns one model instead of Collection of models and null if there is no related model in DB (MorphToMany returns empty Collection in this case). Example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function posts()
    {
        return $this->morphedByMany(Post::class, 'imageable');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function videos()
    {
        return $this->morphedByMany(Video::class, 'imageable');
    }
}
<?php

namespace App\Models;

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

class Post extends Model
{
    use MorphToOne;

    /**
     * Each post has one featured image.
     * 
     * @return \Ankurk91\Eloquent\Relations\MorphToOne
     */
    public function featuredImage()
    {
        return $this->morphToOne(Image::class, 'imageable')
            ->wherePivot('featured', 1);
            //->withDefault();
    }
    
    /**
     * Get all images including the featured.
     *   
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function images()
    {
        return $this->morphToMany(Image::class, 'imageable')
            ->withPivot('featured');
    }

}

Now you can access the relationship like:

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

Testing

composer test

Security

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

Attribution

Most of the code is taken from this PR

License

The MIT License.