Download the PHP package miowng/laravel-eloquent-join without Composer

On this page you can find all versions of the php package miowng/laravel-eloquent-join. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-eloquent-join

Tests status

Laravel Eloquent Join

This package introduces the join magic for eloquent models and relations.

Introduction

Eloquent is a powerful ORM but its join capabilities are very poor.

First Eloquent Problem (sorting)

With laravel you can't perform sorting of the relationship fields without manually joining related table which is very awkward. Let me give you a few reasons why. If you have a table with posts and related categories your code might look like this:

1.The first problem is that you need to worry about select.

Reason : without select id from the category can be selected and hydrated into the Post model.

2.The second problem is that you need to worry about groupBy.

->groupBy('posts.id');

Reason : if the relation is HasOne and there are more than one categories for the post, the query will return more rows for categories.

3.The third problem is that you need to change all other where clauses from :

to

Reason : a post and category can have "date" attribute and in that case without selecting an attribute with table "ambiguous column" error will be thrown.

4.The fourth problem is that you are using table names(not models) and this is also bad and awkward.

5.The fifth problem is that you need to worry about soft deletes for joined tables. If the category is using SoftDeletes trait you must add :

This package will take care of all above problems for you. Unlike sorting, you can perform filtering on the relationship fields without joining related tables, but this package will give you the ability to do this easier.

Second Eloquent Problem (subqueries)

With laravel you can perform where on the relationship attribute but laravel will generate subqueries which are more slower than joins. With this package you will be available to perform where on the relationship with joins in an elegant way.

Requirements

Laravel Version Package Tag Supported Development Branch
>= 5.5.0 4.* yes master
< 5.5.0 - no -

Package is also tested for SQLite, MySql and PostgreSql

Installation & setup

1.Install package with composer

With this statement, a composer will install highest available package version for your current laravel version.

2.Use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoinTrait trait in your base model or only in particular models.

3.IMPORTANT

For MySql make sure that strict configuration is set to false

config/database.php

and that's it, you are ready to go.

Options

Options can be set in the model :

or on query :

useTableAlias

Should we use an alias for joined tables (default = false)

With true query will look like this :

With false query will look like this :

Alias is a randomly generated string.

appendRelationsCount

Should we automatically append relation count field to results (default = false)

With true query will look like this :

Each relation is glued with an underscore and at the end _count prefix is added. For example for

->joinRelations('seller.locations')

field would be seller_locations_count

leftJoin

Should we use inner join or left join (default = true)

vs

aggregateMethod

Which aggregate method to use for ordering (default = 'MAX').

When join is performed on the joined table we must apply aggregate functions on the sorted field so we could perform group by clause and prevent duplication of results.

Options are : SUM, AVG, MAX, MIN, COUNT

Usage

Currently available relations for join queries

New clauses for eloquent builder on BelongsTo and HasOne relations :

joinRelations($relations, $leftJoin = null)

orderByJoin($column, $direction = 'asc', $aggregateMethod = null)

whereJoin($column, $operator, $value, $boolean = 'and')

orWhereJoin($column, $operator, $value)

whereInJoin($column, $values, $boolean = 'and', $not = false)

whereNotInJoin($column, $values, $boolean = 'and')

orWhereInJoin($column, $values)

orWhereNotInJoin($column, $values)

Allowed clauses on BelongsTo, HasOne and HasMany relations on which you can use join clauses on the query

Allowed relation:

Not allowed relation:

The reason why the second relation is not allowed is that this package should apply all those clauses on the join clause, eloquent use all those clauses isolated with subqueries NOT on join clause and that is more simpler to do.

You might get a picture that there are too many rules and restriction, but it is really not like that. Don't worry, if you do create the query that is not allowed appropriate exception will be thrown and you will know what happened.

Other

See action on real example

Database schema :

Database schema

Models :

Join

Join BelongsTo
Join HasOne
Join HasMany
Join Mixed

Join (mix left join)

Join (multiple relationships)

Ordering

Order BelongsTo
Order HasOne
Order HasMany
Order Mixed

Ordering (special cases with aggregate functions)

Order by relation count
Order by relation field SUM
Order by relation field AVG
Order by relation field MAX
Order by relation field MIN

Filtering (where or orWhere)

Filter BelongsTo
Filter HasOne
Filter HasMany
Filter Mixed

Relation count

Filter (mix left join)

Generated queries

Query :

Sql :

Query :

Sql :

Elegance of package

Lets look how first example from documentation now looks like. This code :

is now :

Both snippets do the same thing.

Tests

This package is well covered with tests. If you want run tests just run composer update and then run tests with "vendor/bin/phpunit"

Contribution

Feel free to create new issue for :

License

MIT

Free Software, Hell Yeah!


All versions of laravel-eloquent-join with dependencies

PHP Build Version
Package Version
Requires illuminate/database Version ^5.5|^6.0|^7.0|^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package miowng/laravel-eloquent-join contains the following files

Loading the files please wait ....