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

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

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

Adds MorphToOne relation to Laravel eloquent

1.0.0(1mo ago)01↓50%MITPHPPHP ^8.2

Since Jun 14Pushed 1mo agoCompare

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

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

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

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/d954c3a28f5a820f7d524aaa0ad13a55e251e451b0a25f9d2ce4ab53b04a688b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f646576656c6f7065722f6c61726176656c2d656c6f7175656e742d6d6f7270682d746f2d6f6e652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/jodeveloper/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)

> ⚠️ **Fork Notice** — This repository is a clone of [fidum/laravel-eloquent-morph-to-one](https://github.com/fidum/laravel-eloquent-morph-to-one), published here **only to add support for Laravel 13**. All credit for the original package belongs to [@fidum (Daniel Mason)](https://github.com/fidum). Please use and refer to the upstream project wherever possible. ❤️

📣 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 jodeveloper/laravel-eloquent-morph-to-one
```

Usage
-----

[](#usage)

The `MorphToOne` relation is uses Laravel's [MorphToMany](https://laravel.com/docs/13.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:

```
