PHPackages                             byancode/belongs-to-one - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. byancode/belongs-to-one

ActiveLibrary[Database &amp; ORM](/categories/database)

byancode/belongs-to-one
=======================

Belongs To One Laravel 9 Relation class

v2.0.3(3y ago)06MITPHP

Since May 12Pushed 3y agoCompare

[ Source](https://github.com/byancode/belongs-to-one)[ Packagist](https://packagist.org/packages/byancode/belongs-to-one)[ RSS](/packages/byancode-belongs-to-one/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (7)Used By (0)

Belongs To One Relation for Laravel 9.x
=======================================

[](#belongs-to-one-relation-for-laravel-9x)

[![Travis CI](https://camo.githubusercontent.com/6854dca685b26e81c25d57c0b760736c8336d3f1d7a0881af51520a1d36caf4a/68747470733a2f2f7472617669732d63692e6f72672f4279616e636f64652f62656c6f6e67732d746f2d6f6e652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/6854dca685b26e81c25d57c0b760736c8336d3f1d7a0881af51520a1d36caf4a/68747470733a2f2f7472617669732d63692e6f72672f4279616e636f64652f62656c6f6e67732d746f2d6f6e652e7376673f6272616e63683d6d6173746572)

Based on [Belongs To Many](https://laravel.com/docs/5.3/eloquent-relationships#defining-relationships) Relation. Returns one model instead of Collection of models.

### Installation

[](#installation)

` composer require byancode/belongs-to-one`

### Usage

[](#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:

```
