Giter Club home page Giter Club logo

eager-joins's Introduction

Disclaimer

What this package was originally intended to do has been much better executed with this alternative package: https://github.com/kirschbaum-development/eloquent-power-joins.

One area that I explore here that is lacking from the aforementioned package is the hydration of related models. However my implementation is far from optimized so I would only use this package as a proof-of-concept.

Eager Join

Eager-load your eloquent relationships in Laravel using joins instead of separate queries.

Installation

composer require joeyrush/eager-joins

Setup

  1. Attach the trait to your individual models or base model:
class Post extends Model
{
    use \JoeyRush\EagerJoins\JoinRelations;

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}
  1. Specify which $fields should be loaded in the joins on the related model:
class Category extends Model
{
    /** @var array */
    public $fields = ['id', 'name'];
}

And now you can eager-load using joins by calling the include($relationName) method on your model/query builder wherever you would previously call the with($relationName) method.

// The following will return all posts, each with their associated category pre-loaded.
$posts = Post::include('category')->get();

Examples

Hydrate related models

By default, the relationship fields will be stored on the original model, e.g. the following will return Post models with category_name and category_id stored directly on the model.

$posts = Post::include('category')->get();

If you'd like to have the related models hydrated and linked up correctly, you can call populateRelationships()

Note: the larger the data set, the more of a performance penalty this will incur.

$posts = Post::populateRelationships()->include('category')->get();

Nested relationships

You can use dot notation to eager-load nested relations. This only works on HasOne and BelongsTo relationships

$posts = Post::include('category.creator.profile')->get();

Multiple Eager-Loads

You can eager load as many relationships as needed

$posts = Post::query()
	->include('category.creator.profile')
	->include('tags')
	->include('author')
	->first();

Limitations

  1. You cannot limit or paginate your queries when eager-joining HasMany / BelongsToMany relationships due to SQL limitations
  2. You cannot eager-load nested HasMany or BelongsToMany relationships.
  3. You cannot join on multiple relationships with the same table yet.

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.