zing/laravel-eloquent-relationships
More eloquent relationships for Laravel
137,428
8
| Install | |
|---|---|
composer require zing/laravel-eloquent-relationships |
|
| Latest Version: | 2.4.1 |
| PHP: | ^8.0 |
| Laravel: | 8 – 13 |
| Older releases: | 6 · 7 |
| License: | MIT |
| Last Updated: | Oct 13, 2026 |
| Links: | GitHub · Packagist |
Maintainer: zingimmick
Laravel Eloquent Relationships
Requirements
Require Laravel Eloquent Relationships using Composer:
composer require zing/laravel-eloquent-relationships
Usage
BelongsToOne
BelongsToOne is based on BelongsToMany
Difference:
- returns related model instead of collection of models
- returns
nullinstead of empty collection of models if the relationship does not exist - supports return default related model in case the relationship does not exist
Example:
<?php
use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\BelongsToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\User;
class Group extends Model
{
use HasMoreRelationships;
public function leader(): BelongsToOne
{
return $this->belongsToOne(User::class)
->wherePivot('status', 1)
->withDefault(function (User $user, self $group): void {
$user->name = 'leader for ' . $group->name;
});
}
}
MorphToOne
MorphToOne is based on MorphToMany
Difference:
- returns related model instead of collection of models
- returns
nullinstead of empty collection of models if the relationship does not exist - supports return default related model in case the relationship does not exist
Example:
<?php
use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\Product;
class Image extends Model
{
use HasMoreRelationships;
public function bestProduct(): MorphToOne
{
return $this->morphedByOne(Product::class, 'imageable', 'model_has_images');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Zing\LaravelEloquentRelationships\HasMoreRelationships;
use Zing\LaravelEloquentRelationships\Relations\MorphToOne;
use Zing\LaravelEloquentRelationships\Tests\Models\Image;
class Product extends Model
{
use HasMoreRelationships;
public function cover(): MorphToOne
{
return $this->morphToOne(Image::class, 'imageable', 'model_has_images')->withDefault([
'url' => 'https://example.com/default.png',
]);
}
}
License
Laravel Eloquent Relationships is an open-sourced software licensed under the MIT license.
Related Packages
panakour/laravel-eloquent-relationships
programmatically get the laravel eloquent relationships.
12,123
0
shomisha/unusual-relationships
This package offers a trait which provides your models with the ability to have...
1,041
8
pindab0ter/constrained-morph-to-for-laravel
Constrained polymorphic relationships for Laravel Eloquent - limit morphTo relat...
12,178
3
a2zwebltd/auditable-relations
Automatic auditing for Eloquent relationship changes (attach, detach, sync) usin...
3,519
4
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 2.4.1 | ^8.0 | ^8.69 || | MIT | |
| 2.4.0 | ^8.0 | ^8.69 || | MIT | |
| 2.3.0 | ^8.0 | ^8.69 || | MIT | |
| 2.2.1 | ^8.0 | ^8.69 || | MIT | |
| 2.2.0 | ^8.0 | ^8.69 || | MIT | |
| 2.1.0 | ^8.0 | ^8.69 || | MIT | |
| 2.0.0 | ^8.0 | ^8.69 || | MIT | |
| 1.10.0 | ^7.3 || | ^8.69 || | MIT | |
| 1.9.0 | ^7.3 || | ^8.69 || | MIT | |
| 1.8.0 | ^7.3 || | ^8.35 || | MIT | |
| 1.7.1 | ^7.2 || | ^6.0 || | MIT | |
| 1.7.0 | ^7.2 || | ^6.0 || | MIT | |
| 1.6.0 | ^7.2 || | ^6.0 || | MIT | |
| 1.5.0 | ^7.2 || | ^6.0 || | MIT | |
| 1.4.1 | ^7.2 || | ^6.0 || | MIT | |
| 1.4.0 | ^7.2 || | ^6.0 || | MIT | |
| 1.3.2 | ^7.2 || | ^6.0 || | MIT | |
| 1.3.1 | ^7.2 || | ^6.0 || | MIT | |
| 1.3.0 | ^7.2 || | ^6.0 || | MIT |