2017 © Pedro Peláez
 

library with-join

Package to convert Eloquent BelongsTo subqueries into one query with left join.

image

sleeping-owl/with-join

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  • Wednesday, June 24, 2015
  • by sleeping-owl
  • Repository
  • 13 Watchers
  • 80 Stars
  • 56,882 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 21 Forks
  • 8 Open issues
  • 14 Versions
  • 2 % Grown

The README.md

Package to convert Eloquent BelongsTo subqueries into one query with left join

Build Status Latest Stable Version License Code Climate, (*1)

Usage

Mark relation you want to convert into left join using ->references($relations) method or Model::includes($relations) method:, (*2)

Model::with('other')->references('other')->orderBy('other.title', 'asc')->get();
 # this will make one sql-query with left join of 'other' relation
 # result object will be the same object

Model::includes('first', 'second')->where('first.title', '=', 'my title')->get();
 # will be the same as Model::with('first', 'second')->references('first', 'second')->…

Model extends Eloquent
{
    $includes = ['first', 'second'];
}
Model::where('first.title', '=', 'my title')->get();
# result is same as Model::includes but definition is done within the model
# if you use $with and $includes together it will be merged

Model::with('foreign')->orderBy('field', 'asc')->get();
 # this will work with default behaviour (perform 2 sql-queries)

Example

New Behaviour

StreetImage::includes('street')->first()

will perform the following sql-query:, (*3)

select 
    `street`.`<…>` as `__f__street---<…>`, 
    `street_images`.* 
from 
    `street_images` 
left join 
    `streets` as `street` on `street`.`id` = `street_images`.`street_id` 
order by `sort` asc 
limit 1

Default Behaviour

StreetImage::with('street')->first()

will perform the following sql-queries:, (*4)

select `street_images`.* from `street_images` order by `sort` asc limit 1
select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc

Object Structure

You will get the same object if you will use includes() method. For example:, (*5)

StreetImage::includes('street')->first()

will return:, (*6)

object(StreetImage) {
    <all street image attributes>
    street: object(Street) {
        <all street attributes>
    }
}

Structure will be the same even if you using nested relations:, (*7)

StreetImage::includes('street.district')->first();

will return:, (*8)

object(StreetImage) {
    <all street image attributes>
    street: object(Street) {
        <all street attributes>
        district: object(District) {
            <all district attributes>
        }
    }
}

Nested Relations

StreetImage::includes('street.type', 'street.district')->first();

will perform a following sql-query ( will be replaced with all table columns):, (*9)

select 
     `street`.`<…>` as `__f__street---<…>`,
     `type`.`<…>` as `__f__street---__f__type---<…>`,
     `district`.`<…>` as `__f__street---__f__district---<…>`,
     `street_images`.* 
from 
    `street_images` 
left join 
    `streets` as `street` on `street`.`id` = `street_images`.`street_id` 
left join 
    `street_types` as `type` on `type`.`id` = `street`.`street_type_id` 
left join 
    `districts` as `district` on `district`.`id` = `street`.`district_id` 
order by `sort` asc 
limit 1

instead of performing 4 sql-queries by default Eloquent behaviour:, (*10)

select `street_images`.* from `street_images` order by `sort` asc limit 1

select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc

select * from `street_types` where `street_types`.`id` in (?) order by `title` asc

select * from `districts` where `districts`.`id` in (?) order by `sort` asc

Installation

  1. Require this package in your composer.json and run composer update (or run composer require sleeping-owl/with-join:1.x directly):, (*11)

    "sleeping-owl/with-join": "1.*"
  2. Use \SleepingOwl\WithJoin\WithJoinTrait trait in every eloquent model you want to use this package features:, (*12)

    class StreetImage extends \Eloquent
    {
        use \SleepingOwl\WithJoin\WithJoinTrait;
    }
    
  3. That`s all., (*13)

Support Library

You can donate via PayPal or in BTC: 13k36pym383rEmsBSLyWfT3TxCQMN2Lekd, (*14)

Package was written by Sleeping Owl for the Laravel framework and is released under the MIT License. See the LICENSE file for details., (*15)

The Versions

24/06 2015

dev-master

9999999-dev

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

05/03 2015

dev-laravel-5.0.6

dev-laravel-5.0.6

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

21/02 2015

1.0.11

1.0.11.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

21/02 2015

1.0.10

1.0.10.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

13/02 2015

1.0.9

1.0.9.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

04/02 2015

1.0.8

1.0.8.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

28/01 2015

1.0.7

1.0.7.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

23/01 2015

1.0.6

1.0.6.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

30/11 2014

1.0.5

1.0.5.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

14/11 2014

1.0.4

1.0.4.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

13/11 2014

1.0.3

1.0.3.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

13/11 2014

1.0.2

1.0.2.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent activerecord join with

13/11 2014

1.0.1

1.0.1.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

laravel eloquent activerecord join with

12/11 2014

1.0.0

1.0.0.0

Package to convert Eloquent BelongsTo subqueries into one query with left join.

  Sources   Download

MIT

The Requires

 

laravel eloquent activerecord join with