Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Eloquent Relationships Laravel Package

zing/laravel-eloquent-relationships

View on GitHub
Deep Wiki
Context7

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 null instead 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 null instead 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope