msafadi/laravel-eloquent-join-with
composer require msafadi/laravel-eloquent-join-with and publish the config if needed (php artisan vendor:publish --provider="Safadi\EloquentJoinWith\EloquentJoinWithServiceProvider").use Safadi\EloquentJoinWith\Database\Concerns\JoinWith; to your Eloquent model (e.g., User.php).// Replace eager loading with a single join query
$users = User::joinWith('posts')->get();
posts() is a HasOne or BelongsTo relationship.HasOne/BelongsTo (e.g., public function posts() { return $this->hasOne(Post::class); }).joinWith() method in the trait for available options (e.g., select(), where()).Basic Joins:
// Replace `with('posts')->find(1)` with:
User::joinWith('posts')->find(1);
N+1 eager loading.Conditional Joins:
User::joinWith('posts')->whereHas('posts', function($q) {
$q->where('published', true);
})->get();
whereHas to filter joined tables.Selective Columns:
User::joinWith(['posts' => function($query) {
$query->select('id', 'title');
}])->get();
Nested Joins:
User::joinWith(['posts' => function($query) {
$query->joinWith('comments');
}])->get();
joinWith for deeper relationships.Integration with Existing Code:
with() calls in repositories/services:
// Before:
$user = User::with('posts')->find($id);
// After:
$user = User::joinWith('posts')->find($id);
Dynamic Joins:
$relations = request()->input('relations', []);
User::joinWith($relations)->get();
Hybrid Queries:
User::select('users.*')
->joinWith('posts')
->where('users.active', true)
->get();
joinWith with custom select() clauses.Pagination:
User::joinWith('posts')->paginate(10);
API Resources:
// UserResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'posts' => $this->whenLoaded('posts', fn() => $this->posts),
];
}
Unsupported Relationships:
joinWith only works with HasOne/BelongsTo. Using HasMany/BelongsToMany will fail silently or throw errors.with() for others.Missing Foreign Keys:
public function posts() {
return $this->hasOne(Post::class, 'user_id', 'id');
}
Over-Eager Joins:
DB::enableQueryLog()).Caching Quirks:
remember()).fresh():
$user = User::joinWith('posts')->find($id)->fresh();
Soft Deletes:
whereNull('posts.deleted_at') to the join query.Query Logging:
DB::enableQueryLog();
User::joinWith('posts')->get();
dd(DB::getQueryLog());
Relationship Debugging:
// Check if a relationship is recognized
dd(app(User::class)->getRelationships());
Common Errors:
use Safadi\EloquentJoinWith\Database\Concerns\JoinWith;).->toSql() to inspect the query:
$query = User::joinWith('posts')->toSql();
Custom Join Logic:
joinWith method in your model:
public function scopeCustomJoinWith($query, $relations) {
return $query->joinWith($relations)->select('users.*');
}
User::customJoinWith(['posts'])->get();Global Configuration:
php artisan vendor:publish) to customize:
select clauses.Testing:
$user = new User();
$user->setJoinWithMock(true); // Hypothetical; check package docs for actual method.
Performance Tuning:
joinWith in scopes:
public function scopeWithPosts($query) {
return $query->joinWith('posts');
}
Usage: User::withPosts()->get();How can I help you explore Laravel packages today?