cmxperts/belongs-to-one

Belongs To One Laravel 5 Relation class

v1.0 2023-07-25 05:51 UTC

This package is not auto-updated.

Last update: 2024-05-30 19:45:45 UTC


README

Travis CI

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

Installation

composer require cmxperts/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 cmXperts\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 cmXperts\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