All Projects → mpyw → eloquent-has-by-join

mpyw / eloquent-has-by-join

Licence: MIT license
Convert has() and whereHas() constraints to join() ones for single-result relations.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to eloquent-has-by-join

eloquent-has-by-non-dependent-subquery
Convert has() and whereHas() constraints to non-dependent subqueries.
Stars: ✭ 70 (+233.33%)
Mutual labels:  eloquent, relationships
laravel-arangodb
ArangoDB driver for Laravel
Stars: ✭ 43 (+104.76%)
Mutual labels:  eloquent
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+290.48%)
Mutual labels:  eloquent
db-safedelete
Attempts to invoke force delete, if it fails - falls back to soft delete
Stars: ✭ 16 (-23.81%)
Mutual labels:  eloquent
eloquent-hashids
Automatically generate and persist Hashids for newly created Eloquent models.
Stars: ✭ 17 (-19.05%)
Mutual labels:  eloquent
laravel-hasmanywithinverse
Define HasMany while also setting the inverse relationship in Laravel.
Stars: ✭ 57 (+171.43%)
Mutual labels:  eloquent
tabulator
A set of Unix shell command line tools for quick and convenient batch processing of tabular text files (a.k.a., tab-delimited, tsv, csv, or flat data file format) with a header line. Provides column reference by name, automatic delimiter and compression detection for per-line transformations, sql-like group-by operation and relational join.
Stars: ✭ 34 (+61.9%)
Mutual labels:  join
query-filter
Define filters for your Eloquent models based on your request
Stars: ✭ 20 (-4.76%)
Mutual labels:  eloquent
dilovel
An advanced framework is written in PHP, a framework containing rich components such as middleware, orm, request management, template engine, elasticsearch, template engine, many modern frameworks have been written by adopting clean code principles completely written in accordance with PHP standards. like linux operating system ...All of control…
Stars: ✭ 38 (+80.95%)
Mutual labels:  eloquent
Laravel-Auto-Hard-Deleter
Laravel and Lumen Auto Hard Deleter
Stars: ✭ 34 (+61.9%)
Mutual labels:  eloquent
LazyBelongsToMany
A lightweight implementation of Laravel's belongs To many
Stars: ✭ 23 (+9.52%)
Mutual labels:  eloquent
laravel-scoped-cache
Easily cache items specific to your Eloquent models without having to append the ID.
Stars: ✭ 28 (+33.33%)
Mutual labels:  eloquent
laravel-sti
Single table inheritance with Laravel/Eloquent
Stars: ✭ 16 (-23.81%)
Mutual labels:  eloquent
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (+38.1%)
Mutual labels:  eloquent
eloquent-repository
Repository pattern for Eloquent ORM with focus in cache.
Stars: ✭ 30 (+42.86%)
Mutual labels:  eloquent
laravel-cachable-attributes
Allows to cache attribute accessor values in an easy way.
Stars: ✭ 24 (+14.29%)
Mutual labels:  eloquent
heurist
Core development repository. gitHub: Vsn 6 (2020 - ), Vsn 5 (2018 - 2020), Vsn 4 (2014-2017). Sourceforge: Vsn 3 (2009-2013), Vsn 1 & 2 (2005-2009)
Stars: ✭ 39 (+85.71%)
Mutual labels:  relationships
laravel-auto-morph-map
THIS PACKAGE HAS BEEN DEPRECATED — Automatically alias and map the polymorphic types of Eloquent models.
Stars: ✭ 55 (+161.9%)
Mutual labels:  eloquent
SlimREST
An app skeleton for building a REST API with the Slim PHP Micro-Framework
Stars: ✭ 22 (+4.76%)
Mutual labels:  eloquent
lc-spring-data-r2dbc
An extension of spring-data-r2dbc to provide features such as relationships, joins, cascading save/delete, lazy loading, sequence, schema generation, composite id
Stars: ✭ 30 (+42.86%)
Mutual labels:  relationships

Eloquent Has By Join Build Status Coverage Status Scrutinizer Code Quality

Convert has() and whereHas() constraints to join() ones for single-result relations.

NOTICE: Postgres' optimizer is very smart and covers JOIN optimization for dependent (correlated) subqueries. Therefore, this library is mainly targeted at MySQL which has a poor optimizer.

Requirements

  • PHP: ^7.3 || ^8.0
  • Laravel: ^6.0 || ^7.0 || ^8.0 || ^9.0

Installing

composer require mpyw/eloquent-has-by-join

Motivation

Suppose you have the following relationship:

class Post extends Model
{
    use SoftDeletes;
}
class Comment extends Model
{
    use SoftDeletes;

    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }
}

If you use has() constraints, your actual query would have dependent subqueries.

$comments = Comment::has('post')->get();
select * from `comments` where exists (
  select * from `posts`
  where `comments`.`post_id` = `posts`.`id`
    and `posts`.`deleted_at` is null
) and `comments`.`deleted_at` is null

These subqueries may cause performance degradations. This package provides Illuminate\Database\Eloquent\Builder::hasByJoin() macro to solve this problem: you can easily transform dependent subqueries into simple JOIN queries.

$comments = Comment::hasByJoin('post')->get();
select `comments`.* from `comments`
inner join `posts`
        on `comments`.`post_id` = `posts`.`id`
       and `posts`.`deleted_at` is null
where `comments`.`deleted_at` is null

API

Signature

Illuminate\Database\Eloquent\Builder::hasByJoin(string|string[] $relationMethod, ?callable ...$constraints): $this

Arguments

$relationMethod

A relation method name that returns a BelongsTo, HasOne or MorphOne instance.

Builder::hasByJoin('post')

You can pass nested relations as an array or a string with dot-chain syntax.

Builder::hasByJoin(['post', 'author'])
Builder::hasByJoin('post.author')

You can provide table aliases with "as" syntax.

Builder::hasByJoin(['post as messages', 'author as message_authors'])

$constraints

Additional callable constraints for relations that take Illuminate\Database\Eloquent\Builder as the first argument.

Builder::hasByJoin('post', fn (Builder $query) => $query->withTrashed())

The first closure corresponds to post and the second one corresponds to author.

Builder::hasByJoin(
    'post.author',
    fn (Builder $query) => $query->withTrashed(),
    fn (Builder $query) => $query->whereKey(123)
)

Feature Comparison

Feature mpyw/eloquent-has-by-join mpyw/eloquent-has-by-non-dependent-subquery
Minimum Laravel version 5.6 5.8
Argument of optional constraints Illuminate\Database\Eloquent\Builder Illuminate\Database\Eloquent\Relations\*
(Builder can be also accepted by specifying argument type)
Compoships support
No subqueries
(Performance depends on database optimizers)
No table collisions
(Sometimes you need to give aliases)
No column collisions
(Sometimes you need to use qualified column names)
OR conditions
Negative conditions
Counting conditions
HasOne
HasMany
BelongsTo
BelongsToMany
MorphOne
MorphMany
MorphTo
MorphMany
MorphToMany
HasOneThrough
HasManyThrough
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].