MEGApixel23 / belongs-to-one by MEGApixel23

Belongs To One Laravel 5 Relation class
1,584
8
3
Package Data
Maintainer Username: MEGApixel23
Maintainer Contact: megapixel23@gmail.com (megapixel23)
Package Create Date: 2016-11-27
Package Last Update: 2017-12-16
Language: PHP
License: MIT
Last Refreshed: 2024-04-24 03:03:18
Package Statistics
Total Downloads: 1,584
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 8
Total Watchers: 3
Total Forks: 2
Total Open Issues: 1

Belongs To One Relation for Laravel 5.5

Travis CI

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

Installation

composer require megapixel23/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 Megapixel23\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 Megapixel23\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