PHP code example of mpyw / eloquent-has-by-join

1. Go to this page and download the library: Download mpyw/eloquent-has-by-join 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/ */

    

mpyw / eloquent-has-by-join example snippets


class Post extends Model
{
    use SoftDeletes;
}

class Comment extends Model
{
    use SoftDeletes;

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

$comments = Comment::has('post')->get();

$comments = Comment::hasByJoin('post')->get();

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

Builder::hasByJoin('post')

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

Builder::hasByJoin('post.author')

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

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

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