PHP code example of kirschbaum-development / eloquent-power-joins

1. Go to this page and download the library: Download kirschbaum-development/eloquent-power-joins 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/ */

    

kirschbaum-development / eloquent-power-joins example snippets


User::select('users.*')->join('posts', 'posts.user_id', '=', 'users.id');

User::joinRelationship('posts');

User::joinRelationship('posts.comments');

User::leftJoinRelationship('posts.comments');
User::rightJoinRelationship('posts.comments');

Post::joinRelationship('images');

Image::joinRelationship('imageable', morphable: Post::class);

User::joinRelationship('posts', fn ($join) => $join->where('posts.approved', true))->toSql();

User::joinRelationship('posts', fn ($join) => $join->left());

User::joinRelationship('posts.comments', [
    'posts' => fn ($join) => $join->where('posts.published', true),
    'comments' => fn ($join) => $join->where('comments.approved', true),
]);

User::joinRelationship('groups', [
    'groups' => [
        'groups' => function ($join) {
            // ...
        },
        // group_members is the intermediary table here
        'group_members' => fn ($join) => $join->where('group_members.active', true),
    ]
]);

    public function scopePublished($query)
    {
        $query->where('published', true);
    }

User::joinRelationship('posts', function ($join) {
    // the $join instance here can access any of the scopes defined in Post 🤯
    $join->published();
});

Post::joinRelationshipUsingAlias('category.parent')->get();

Post::joinRelationshipUsingAlias('category', 'category_alias')->get();

Post::joinRelationship('category.parent', [
    'category' => fn ($join) => $join->as('category_alias'),
    'parent' => fn ($join) => $join->as('category_parent'),
])->get()

Group::joinRelationship('posts.user', [
    'posts' => [
        'posts' => fn ($join) => $join->as('posts_alias'),
        'post_groups' => fn ($join) => $join->as('post_groups_alias'),
    ],
])->toSql();

User::joinRelationship('posts')->toSql();
// select users.* from users inner join posts on posts.user_id = users.id

User::select('users.id')->joinRelationship('posts')->toSql();
// select users.id from users inner join posts on posts.user_id = users.id

UserProfile::joinRelationship('users', fn ($join) => $join->withTrashed());

UserProfile::joinRelationship('users', ($join) => $join->onlyTrashed());

class User extends Model
{
    public function publishedPosts()
    {
        return $this->hasMany(Post::class)->published();
    }
}

UserProfile::joinRelationship('users', fn ($join) => $join->withGlobalScopes());

User::powerJoinHas('posts');
User::powerJoinHas('posts.comments');
User::powerJoinHas('posts.comments', '>', 3);
User::powerJoinWhereHas('posts', function ($join) {
    $join->where('posts.published', true);
});
User::powerJoinDoesntHave('posts');

User::powerJoinWhereHas('commentsThroughPosts', [
    'comments' => fn ($query) => $query->where('body', 'a')
])->get());

User::orderByPowerJoins('profile.city');

User::orderByPowerJoins(['profile', DB::raw('concat(city, ", ", state)')]);

$users = User::orderByPowerJoinsCount('posts.id', 'desc')->get();

$posts = Post::orderByPowerJoinsAvg('comments.votes', 'desc')->get();

Post::orderByPowerJoinsSum('comments.votes');
Post::orderByPowerJoinsMin('comments.votes');
Post::orderByPowerJoinsMax('comments.votes');

Post::orderByLeftPowerJoinsCount('comments.votes');
Post::orderByLeftPowerJoinsAvg('comments.votes');
Post::orderByLeftPowerJoinsSum('comments.votes');
Post::orderByLeftPowerJoinsMin('comments.votes');
Post::orderByLeftPowerJoinsMax('comments.votes');