All Projects → mpyw → eloquent-has-by-non-dependent-subquery

mpyw / eloquent-has-by-non-dependent-subquery

Licence: MIT License
Convert has() and whereHas() constraints to non-dependent subqueries.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to eloquent-has-by-non-dependent-subquery

eloquent-has-by-join
Convert has() and whereHas() constraints to join() ones for single-result relations.
Stars: ✭ 21 (-70%)
Mutual labels:  eloquent, relationships
laravel-repository
Repository pattern implementation for Laravel
Stars: ✭ 49 (-30%)
Mutual labels:  eloquent
laravel-nova-nested-form
This package allows you to include your nested relationships' forms into a parent form.
Stars: ✭ 225 (+221.43%)
Mutual labels:  relationships
laravel-local-class-scope
A tiny macro that reuse a global scope class as a local scope
Stars: ✭ 16 (-77.14%)
Mutual labels:  eloquent
laravel-mass-update
Update multiple Laravel Model records, each with its own set of values, sending a single query to your database!
Stars: ✭ 65 (-7.14%)
Mutual labels:  eloquent
laravel-parallel
Laravel parallel
Stars: ✭ 41 (-41.43%)
Mutual labels:  eloquent
query-filter
Define filters for your Eloquent models based on your request
Stars: ✭ 20 (-71.43%)
Mutual labels:  eloquent
eloquent-phpunit
Eloquent model and database schema PHPUnit test case
Stars: ✭ 20 (-71.43%)
Mutual labels:  eloquent
MercadoLivreProductsCrawler
PHP Console Crawler to Download Products from a Store on MercadoLivre.com.br
Stars: ✭ 18 (-74.29%)
Mutual labels:  eloquent
pimpable
No description or website provided.
Stars: ✭ 102 (+45.71%)
Mutual labels:  eloquent
laravel-searchzy
Package que te permite buscar y filtrar registros de Eloquent Models en Laravel de una manera simple y sencilla.
Stars: ✭ 15 (-78.57%)
Mutual labels:  eloquent
laravel-tmdb
Interact with TMDB data in your Laravel application.
Stars: ✭ 25 (-64.29%)
Mutual labels:  eloquent
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (-67.14%)
Mutual labels:  eloquent
thinkorm
A flexible, lightweight and powerful Object-Relational Mapper for Node.js. Support TypeScript!!
Stars: ✭ 33 (-52.86%)
Mutual labels:  relationships
laravel-route-model-autobinding
THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.
Stars: ✭ 14 (-80%)
Mutual labels:  eloquent
SlimREST
An app skeleton for building a REST API with the Slim PHP Micro-Framework
Stars: ✭ 22 (-68.57%)
Mutual labels:  eloquent
laravel-eloquent-spatial
Laravel Eloquent spatial package.
Stars: ✭ 90 (+28.57%)
Mutual labels:  eloquent
authorized-attributes
Authorized Model Attributes for Laravel
Stars: ✭ 22 (-68.57%)
Mutual labels:  eloquent
userstamps
A simple package to insert and load userstamps for a model automatically, it provides an eloquent trait to use in models..
Stars: ✭ 34 (-51.43%)
Mutual labels:  eloquent
laravel-eloquent-relationships-gravit-designer
Graphic showing Laravel Eloquent ORM Relationships
Stars: ✭ 19 (-72.86%)
Mutual labels:  eloquent

Eloquent Has By Non-dependent Subquery Build Status Coverage Status Scrutinizer Code Quality

Convert has() and whereHas() constraints to non-dependent subqueries.

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-non-dependent-subquery

Suggestion

You can install wimski/laravel-ide-helper-hook-eloquent-has-by-non-dependent-subquery to work with Laravel IDE Helper.

Motivation

Suppose you have the following relationship:

class Post extends Model
{
    use SoftDeletes;

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }
}
class Comment extends Model
{
    use SoftDeletes;
}

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

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

These subqueries may cause performance degradations. This package provides Illuminate\Database\Eloquent\Builder::hasByNonDependentSubquery() macro to solve this problem: you can easily transform dependent subqueries into non-dependent ones.

$posts = Post::hasByNonDependentSubquery('comments')->get();
select * from `posts`
where `posts`.`id` in (
  select `comments`.`post_id` from `comments`
  where `comments`.`deleted_at` is null
)
and `posts`.`deleted_at` is null

API

Signature

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

Arguments

$relationMethod

A relation method name that returns a Relation instance except MorphTo.

Builder::hasByNonDependentSubquery('comments')

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

Builder::hasByNonDependentSubquery(['comments', 'author'])
Builder::hasByNonDependentSubquery('comments.author')

$constraints

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

Builder::hasByNonDependentSubquery('comments', fn (HasMany $query) => $query->withTrashed())

If you are using a union type as of PHP 8.0, the order of types does not matter.

// This will work
Builder::hasByNonDependentSubquery('comments', fn (HasMany|Comment $query) => $query->withTrashed())
// and so will this
Builder::hasByNonDependentSubquery('comments', fn (Comment|HasMany $query) => $query->withTrashed())

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

Builder::hasByNonDependentSubquery(
    'comments.author',
    fn (HasMany $query) => $query->withTrashed(),
    fn (BelongsTo $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].