LaravelPackages.net
Acme Inc.
Toggle sidebar
byancode/belongs-to-one

Belongs To One Laravel 9 Relation class

6
0
v2.0.3
About byancode/belongs-to-one

byancode/belongs-to-one is a Laravel package for belongs to one laravel 9 relation class. It currently has 0 GitHub stars and 6 downloads on Packagist (latest version v2.0.3). Install it with composer require byancode/belongs-to-one. Discover more Laravel packages by byancode or browse all Laravel packages to compare alternatives.

Last updated

Belongs To One Relation for Laravel 9.x

Travis CI

Based on Belongs To Many Relation. Returns one model instead of Collection of models.

Installation

composer require byancode/belongs-to-one

Usage

BelongsToOne relation is almost identical to standard BelongsToMany except it returns one model instead of Collection of models and null if there is no related model in DB (BelongsToMany returns empty Collection in this case). Include Byancode\Database\Eloquent\Relations\BelongsToOneTrait within your Model and use BelongsToOne relation.

Example:

<?php

namespace App\Models;

use App\Models\Group;
Use Illuminate\Database\Eloquent\Model;
use Byancode\Database\Eloquent\Relations\BelongsToOneTrait;

class Record extends Model
{
    use BelongsToOneTrait;
    
    public function group()
    {
        return $this->belongsToOne(Group::class);
    }
}

And then use it like any other relation:

$record = Record::with('group')->first();

Testing

./vendor/bin/phpunit

Comments