GithubHelp home page GithubHelp logo

laravel-eloquent-relationship's Introduction

Missing Eloquent Relationships For Laravel

Packagist GitHub tag License Downloads tests codecov

This package adds some missing relationships to Eloquent in Laravel

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\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

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;
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');

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.

laravel-eloquent-relationship's People

Contributors

antonparussimov avatar ankurk91 avatar

Stargazers

 avatar apple.yoichi avatar Fullstack804 avatar Huevo avatar AlexHolden avatar Captain avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.