PHPackages                             kaveh/morph-service - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. kaveh/morph-service

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kaveh/morph-service
===================

1.0.0(1y ago)08PHPPHP ^8.3

Since Feb 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kaveh1999nazari/laravel-morph-service)[ Packagist](https://packagist.org/packages/kaveh/morph-service)[ RSS](/packages/kaveh-morph-service/feed)WikiDiscussions main Synced today

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

Laravel Morph Service 📦
=======================

[](#laravel-morph-service-)

Overview
--------

[](#overview)

The **Laravel Morph Service** package simplifies adding media files to multiple models without the need for separate media tables for each model. Instead, it provides a unified media table that associates files with different models dynamically.

This is achieved using `mediable_id` and `mediable_type`, which store the ID and type of the model that owns the media file. For example, if you want to store a profile picture for a `User`, the table will store the `user_id` as `mediable_id` and `User` as `mediable_type`. This structure makes it easy to retrieve media files related to specific models while maintaining a clean and structured database design.

---

🔧 Installation
--------------

[](#-installation)

To install the package, run the following command:

```
composer require kaveh/morph-service
```

Then, add the provider in `bootstrap/provider.php`:

```
Kaveh\LaravelMorphService\MorphServiceProvider::class,
```

To ensure the package integrates properly with your project, add the following to `composer.json`:

```
"autoload": {
   "psr-4": {
       "Kaveh\\NotificationService\\": "vendor/kaveh/notification-service/src/"
   }
}
```

Then, run:

```
composer dump-autoload
```

---

📌 Database Migration
--------------------

[](#-database-migration)

Run the migration command to set up the required tables:

```
php artisan morph:migrate
```

---

📂 Usage
-------

[](#-usage)

### 1️⃣ Define the Media Relationship

[](#1️⃣-define-the-media-relationship)

For any model that needs to support media files, add the following method:

```
use Illuminate\Database\Eloquent\Relations\MorphMany;
use App\Models\Media;

public function medias(): MorphMany
{
    return $this->morphMany(Media::class, 'mediable');
}
```

---

### 2️⃣ Upload Media in Your Controller

[](#2️⃣-upload-media-in-your-controller)

To upload a file and associate it with a model, use the following:

```
use Kaveh\LaravelMorphService\Services\MediaService;
use Illuminate\Http\UploadedFile;

// Upload media
MediaService::uploadMedia(instance of UploadedFile, instance of Model);
```

---

🎯 Benefits
----------

[](#-benefits)

✔ **Single media table for all models** – No need for separate media tables.
✔ **Flexible association** – Easily attach media to different models dynamically.
✔ **Efficient querying** – Retrieve media files related to any model without extra joins.

---

This package streamlines media management in Laravel, making file storage more efficient and scalable! 🚀

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance40

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

502d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/43339699?v=4)[kaveh1999](/maintainers/kaveh1999)[@kaveh1999](https://github.com/kaveh1999)

---

Top Contributors

[![kaveh1999nazari](https://avatars.githubusercontent.com/u/149464995?v=4)](https://github.com/kaveh1999nazari "kaveh1999nazari (7 commits)")

### Embed Badge

![Health badge](/badges/kaveh-morph-service/health.svg)

```
[![Health](https://phpackages.com/badges/kaveh-morph-service/health.svg)](https://phpackages.com/packages/kaveh-morph-service)
```

###  Alternatives

[markwalet/nova-modal-response

A Laravel Nova asset for Modal responses on an action.

17878.9k](/packages/markwalet-nova-modal-response)[crumbls/layup

A visual page builder plugin for Filament 5 — Divi-style grid layouts with extensible widgets.

592.7k2](/packages/crumbls-layup)[team-nifty-gmbh/tall-datatables

Server-side rendered datatables for Laravel and Livewire

1320.9k4](/packages/team-nifty-gmbh-tall-datatables)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
