PHPackages                             fidum/laravel-eloquent-morph-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. fidum/laravel-eloquent-morph-to-one

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

fidum/laravel-eloquent-morph-to-one
===================================

Adds MorphToOne relation to Laravel eloquent

2.5.0(1y ago)31136.1k↑33.8%32MITPHPPHP ^8.2CI passing

Since Jun 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/fidum/laravel-eloquent-morph-to-one)[ Packagist](https://packagist.org/packages/fidum/laravel-eloquent-morph-to-one)[ Docs](https://github.com/fidum/laravel-eloquent-morph-to-one)[ GitHub Sponsors](https://github.com/dmason30)[ RSS](/packages/fidum-laravel-eloquent-morph-to-one/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (12)Used By (2)

Adds MorphToOne relation to Laravel eloquent
============================================

[](#adds-morphtoone-relation-to-laravel-eloquent)

[![Latest Version on Packagist](https://camo.githubusercontent.com/52ab759690af49d6fd45d8d3bfe3082e65d0d4edd2770ba1e637e2288bf3779f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666964756d2f6c61726176656c2d656c6f7175656e742d6d6f7270682d746f2d6f6e652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/fidum/laravel-eloquent-morph-to-one)[![GitHub Workflow Status (with branch)](https://camo.githubusercontent.com/31862bf344e9d9c49ffa07ef063ace936205066949cbff904cedc5d187d94ee4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666964756d2f6c61726176656c2d656c6f7175656e742d6d6f7270682d746f2d6f6e652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666f722d7468652d6261646765)](https://github.com/fidum/laravel-eloquent-morph-to-one/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Codecov](https://camo.githubusercontent.com/79742b903a5f8b89abbfca8b28c5396474eaa91ea95217863a60e90c843199c9/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f666964756d2f6c61726176656c2d656c6f7175656e742d6d6f7270682d746f2d6f6e653f6c6f676f3d636f6465636f76266c6f676f436f6c6f723d7768697465267374796c653d666f722d7468652d6261646765)](https://codecov.io/gh/fidum/laravel-eloquent-morph-to-one)[![Twitter Follow](https://camo.githubusercontent.com/3ecd70148f9f16e2e0d66fcde3428926f1df4d6797ea463473ccf60dcdf7758a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f666f6c6c6f772d25343064616e6d61736f6e6d702d3144413146323f6c6f676f3d74776974746572267374796c653d666f722d7468652d6261646765)](https://twitter.com/danmasonmp)

📣 Shoutout to [Ankur Kumar](https://github.com/ankurk91) who wrote the original code for this relation in their package [Eloquent Relations](https://github.com/ankurk91/laravel-eloquent-relationships). I created this package as I needed to make a few tweaks to suit my needs. 🙌

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require fidum/laravel-eloquent-morph-to-one
```

Usage
-----

[](#usage)

The `MorphToOne` relation is uses Laravel's [MorphToMany](https://laravel.com/docs/7.x/eloquent-relationships#many-to-many-polymorphic-relations) under the hood. However, the difference is that it returns **one** model instead of a `Collection` of models.

If there is no related model in the database it will return `null` where `MorphToMany` would return an empty `Collection`.

Example:

```
