### Install Eloquent Power Joins Package via Composer Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This command installs the latest stable version of the Eloquent Power Joins package using Composer. It's the standard way to add the package to your Laravel project. ```bash composer require kirschbaum-development/eloquent-power-joins ``` -------------------------------- ### Install Eloquent Power Joins for Older Laravel Versions Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This command installs a specific version of the Eloquent Power Joins package compatible with older Laravel versions (e.g., 3.* for Laravel < 10, 2.* for Laravel < 8). Use this if your project is not on the latest Laravel release. ```bash composer require kirschbaum-development/eloquent-power-joins:3.* ``` -------------------------------- ### Querying Relationship Existence with Joins (Laravel vs. Power Joins) Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Compares Laravel's native `where exists` methods for querying relationship existence with the `eloquent-power-joins` package's join-based equivalents. It highlights the performance implications and differences in results. ```php User::has('posts'); User::has('posts.comments'); User::has('posts', '>', 3); User::whereHas('posts', fn ($query) => $query->where('posts.published', true)); User::whereHas('posts.comments', ['posts' => fn ($query) => $query->where('posts.published', true)); User::doesntHave('posts'); ``` ```php 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'); ``` ```php User::powerJoinWhereHas('commentsThroughPosts', [ 'comments' => fn ($query) => $query->where('body', 'a') ])->get()); ``` -------------------------------- ### Join Nested Eloquent Relationships with Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This snippet demonstrates joining nested relationships using `joinRelationship()`. It joins 'posts' and then 'comments' related to those posts, simplifying complex multi-level joins by leveraging defined Eloquent relationships. ```php User::joinRelationship('posts.comments'); ``` -------------------------------- ### Applying Conditions and Callbacks to Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This section demonstrates how to apply custom conditions or specify join types by passing a callback as the second parameter to 'joinRelationship'. It also covers how to apply conditions to nested relationships and belongs-to-many joins using arrays. ```php User::joinRelationship('posts', fn ($join) => $join->where('posts.approved', true))->toSql(); ``` ```php User::joinRelationship('posts', fn ($join) => $join->left()); ``` ```php User::joinRelationship('posts.comments', [ 'posts' => fn ($join) => $join->where('posts.published', true), 'comments' => fn ($join) => $join->where('comments.approved', true), ]); ``` ```php User::joinRelationship('groups', [ 'groups' => [ 'groups' => function ($join) { // ... }, // group_members is the intermediary table here 'group_members' => fn ($join) => $join->where('group_members.active', true), ] ]); ``` -------------------------------- ### Join Eloquent Relationships with Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This snippet uses the `joinRelationship()` method provided by the Eloquent Power Joins package to join the 'posts' table to the 'users' table. It leverages the defined Eloquent relationship, making the code more readable and abstracting implementation details. ```php User::joinRelationship('posts'); ``` -------------------------------- ### Enabling Global Scopes in Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Shows how to enable global scopes on joined models using the `withGlobalScopes()` method in the join clause. It also notes a limitation regarding type-hinting `Eloquent\Builder` in the global scope's `apply` method. ```php UserProfile::joinRelationship('users', fn ($join) => $join->withGlobalScopes()); ``` -------------------------------- ### Handling Soft Deletes in Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Explains how `eloquent-power-joins` automatically applies soft delete conditions and how to include or only include trashed models using `withTrashed()` or `onlyTrashed()` methods in the join callback. ```sql and "users"."deleted_at" is null ``` ```php UserProfile::joinRelationship('users', fn ($join) => $join->withTrashed()); ``` ```php UserProfile::joinRelationship('users', ($join) => $join->onlyTrashed()); ``` -------------------------------- ### Applying Extra Conditions from Eloquent Relationships Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Demonstrates how `eloquent-power-joins` automatically applies additional conditions defined within Eloquent relationship methods, such as a `published()` scope on a `hasMany` relationship. ```php class User extends Model { public function publishedPosts() { return $this->hasMany(Post::class)->published(); } } ``` ```sql select users.* from users inner join posts on posts.user_id = posts.id and posts.published = 1 ``` -------------------------------- ### Ordering Query Results by Related Table Columns Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Explains how to sort query results using columns from joined tables with `orderByPowerJoins`. It also covers sorting by raw SQL expressions and aggregate functions like `COUNT`, `SUM`, `AVG`, `MIN`, and `MAX`, including left join variants. ```php User::orderByPowerJoins('profile.city'); ``` ```php User::orderByPowerJoins(['profile', DB::raw('concat(city, ", ", state)')]); ``` ```php $users = User::orderByPowerJoinsCount('posts.id', 'desc')->get(); ``` ```php $posts = Post::orderByPowerJoinsAvg('comments.votes', 'desc')->get(); ``` ```php Post::orderByPowerJoinsSum('comments.votes'); Post::orderByPowerJoinsMin('comments.votes'); Post::orderByPowerJoinsMax('comments.votes'); ``` ```php Post::orderByLeftPowerJoinsCount('comments.votes'); Post::orderByLeftPowerJoinsAvg('comments.votes'); Post::orderByLeftPowerJoinsSum('comments.votes'); Post::orderByLeftPowerJoinsMin('comments.votes'); Post::orderByLeftPowerJoinsMax('comments.votes'); ``` -------------------------------- ### Perform Left or Right Joins on Eloquent Relationships Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md These snippets show how to perform `left` and `right` joins on Eloquent relationships using the `leftJoinRelationship()` and `rightJoinRelationship()` methods provided by the package. This allows for more flexible join types while maintaining the 'Laravel way' of joining relationships. ```php User::leftJoinRelationship('posts.comments'); User::rightJoinRelationship('posts.comments'); ``` -------------------------------- ### Perform SQL Join on User and Post Tables (Traditional) Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md This snippet demonstrates a traditional SQL join operation in Laravel Eloquent, explicitly joining the 'posts' table to the 'users' table based on their foreign key relationship. It selects all columns from the 'users' table. ```php User::select('users.*')->join('posts', 'posts.user_id', '=', 'users.id'); ``` -------------------------------- ### Using Model Scopes within Eloquent Power Join Callbacks Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Eloquent Power Joins allows developers to leverage existing model scopes directly within join callbacks, simplifying the application of complex conditions. Note that the '$query' parameter in the scope cannot be type-hinted, and only join-supported conditions are applicable. ```php public function scopePublished($query) { $query->where('published', true); } ``` ```php User::joinRelationship('posts', function ($join) { // the $join instance here can access any of the scopes defined in Post 🤯 $join->published(); }); ``` -------------------------------- ### Managing Table Aliases in Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md When joining the same table multiple times, aliases are crucial to prevent conflicts. Eloquent Power Joins provides 'joinRelationshipUsingAlias' for simple aliasing and allows more granular control via the 'as' function within join callbacks for nested or complex relationships. ```php Post::joinRelationshipUsingAlias('category.parent')->get(); ``` ```php Post::joinRelationshipUsingAlias('category', 'category_alias')->get(); ``` ```php Post::joinRelationship('category.parent', [ 'category' => fn ($join) => $join->as('category_alias'), 'parent' => fn ($join) => $join->as('category_parent'), ])->get() ``` ```php Group::joinRelationship('posts.user', [ 'posts' => [ 'posts' => fn ($join) => $join->as('posts_alias'), 'post_groups' => fn ($join) => $join->as('post_groups_alias'), ], ])->toSql(); ``` -------------------------------- ### Automatic Column Selection with Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md To prevent column name conflicts when using 'SELECT *' with joins, Eloquent Power Joins automatically selects only the parent table's columns if no specific columns are selected beforehand. If columns are explicitly selected, the package respects that selection. ```php User::joinRelationship('posts')->toSql(); ``` ```php User::select('users.id')->joinRelationship('posts')->toSql(); ``` -------------------------------- ### Joining Polymorphic Relationships with Eloquent Power Joins Source: https://github.com/kirschbaum-development/eloquent-power-joins/blob/master/README.md Eloquent Power Joins automatically handles the 'where imageable_type' condition when joining polymorphic relationships, simplifying queries. It supports both 'morphMany' and 'morphTo' relationships, though 'morphTo' is limited to one morphable type at a time. ```php Post::joinRelationship('images'); ``` ```php Image::joinRelationship('imageable', morphable: Post::class); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.