PHP code example of reedware / laravel-relation-joins
1. Go to this page and download the library: Download reedware/laravel-relation-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/ */
reedware / laravel-relation-joins example snippets
User::query()->joinRelation('posts', function ($join) {
$join->where('posts.created_at', '>=', '2019-01-01');
});
// Using the "active" query scope on the "Post" model
User::query()->joinRelation('posts', function ($join) {
$join->active();
});
// Disabling soft deletes for only the "Post" model
User::query()->joinRelation('posts', function ($join) {
$join->withTrashed();
});
// Adding pivot ("role_user") constraints for a "Belongs to Many" relation
User::query()->joinRelation('roles', function ($join, $pivot) {
$pivot->where('domain', '=', 'web');
});
// Adding pivot ("users") constraints for a "Has Many Through" relation
Country::query()->joinRelation('posts', function ($join, $through) {
$through->where('is_admin', '=', true);
});
// Using a query scope for the intermediate "RoleUser" pivot in a "Belongs to Many" relation
User::query()->joinRelation('roles', function ($join, $pivot) {
$pivot->web();
});
// Disabling soft deletes for the intermediate "User" model
Country::query()->joinRelation('posts', function ($join, $through) {
$through->withTrashed();
});
User::query()->joinRelation('posts.comments', [
function ($join) { $join->where('is_active', '=', 1); },
function ($join) { $join->where('comments.title', 'like', '%looking for something%'); }
});
// Sequential
User::query()->joinRelation('posts.comments', [
null,
function ($join) { $join->where('comments.title', 'like', '%looking for something%'); }
});
// Associative
User::query()->joinRelation('posts.comments', [
'comments' => function ($join) { $join->where('comments.title', 'like', '%looking for something%'); }
});
User::query()->joinRelation('posts as articles.comments as threads', [
'posts as articles' => function ($join) { $join->where('is_active', '=', 1); },
'comments as threads' => function ($join) { $join->where('threads.title', 'like', '%looking for something%'); }
});
User::query()->joinRelation('posts', function ($join) {
$join->where('is_active', '=', 1);
})->joinThroughRelation('posts.comments', function ($join) {
$join->where('comments.title', 'like', '%looking for something%');
});
public function employees()
{
return $this->hasMany(static::class, 'manager_id', 'id');
}
User::query()->joinRelation('employees');
// SQL: select * from "users" inner join "users" as "laravel_reserved_0" on "laravel_reserved_0"."manager_id" = "users"."id"
User::query()->joinRelation('employees as employees');
// SQL: select * from "users" inner join "users" as "employees" on "employees"."manager_id" = "users"."id"
User::query()->joinRelation('posts as articles');
// SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id"
User::query()->joinRelation('posts as articles.comments as feedback');
// SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id" inner join "comments" as "feedback" on "feedback"."post_id" = "articles"."id"
public function roles()
{
return $this->belongsToMany(EloquentRoleModelStub::class, 'role_user', 'user_id', 'role_id');
}
User::query()->joinRelation('roles as users_roles,roles');
// SQL: select * from "users" inner join "role_user" as "users_roles" on "users_roles"."user_id" = "users"."id" inner join "roles" on "roles"."id" = "users_roles"."role_id"
User::query()->joinRelation('roles as users_roles,positions');
// SQL: select * from "users" inner join "role_user" as "position_user" on "position_user"."user_id" = "users"."id" inner join "roles" as "positions" on "positions"."id" = "position_user"."role_id"
Image::query()->joinMorphRelation('imageable', Post::class);
// SQL: select * from "images" inner join "posts" on "posts"."id" = "images"."imageable_id" and "images"."imageable_type" = ?