PHP code example of homedoctor-es / eloquent-has-by-non-dependent-subquery

1. Go to this page and download the library: Download homedoctor-es/eloquent-has-by-non-dependent-subquery library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

homedoctor-es / eloquent-has-by-non-dependent-subquery example snippets


class Post extends Model
{
    use SoftDeletes;

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

class Comment extends Model
{
    use SoftDeletes;
}

$posts = Post::has('comments')->get();

$posts = Post::hasByNonDependentSubquery('comments')->get();

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

Builder::hasByNonDependentSubquery('comments')

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

Builder::hasByNonDependentSubquery('comments.author')

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

// 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())

Builder::hasByNonDependentSubquery(
    'comments.author',
    fn (HasMany $query) => $query->withTrashed(),
    fn (BelongsTo $query) => $query->whereKey(123)
)