{"id":20812177,"url":"https://github.com/sertxudeveloper/laravel-media-model","last_synced_at":"2025-08-29T13:33:38.540Z","repository":{"id":52411667,"uuid":"498818555","full_name":"sertxudeveloper/laravel-media-model","owner":"sertxudeveloper","description":"Attach media files to your Eloquent models","archived":false,"fork":false,"pushed_at":"2024-10-07T09:21:32.000Z","size":533,"stargazers_count":11,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-10T03:59:29.304Z","etag":null,"topics":["attachments","eloquent","hacktoberfest","laravel","laravel-model","laravel-package","media","sertxudeveloper"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sertxudeveloper.png","metadata":{"funding":{"github":["sertxudev"],"custom":["https://buymeacoffee.com/sertxudeveloper"]},"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-01T16:41:22.000Z","updated_at":"2024-06-21T06:32:48.000Z","dependencies_parsed_at":"2023-01-21T20:19:31.305Z","dependency_job_id":"9cf5ec82-ad75-4a6b-ba2a-02f03b60a7b7","html_url":"https://github.com/sertxudeveloper/laravel-media-model","commit_stats":{"total_commits":31,"total_committers":2,"mean_commits":15.5,"dds":0.09677419354838712,"last_synced_commit":"fdadb93a5d42b6bf613ff1d88ddb6146e3514415"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sertxudeveloper%2Flaravel-media-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sertxudeveloper%2Flaravel-media-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sertxudeveloper%2Flaravel-media-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sertxudeveloper%2Flaravel-media-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sertxudeveloper","download_url":"https://codeload.github.com/sertxudeveloper/laravel-media-model/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225090568,"owners_count":17419443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attachments","eloquent","hacktoberfest","laravel","laravel-model","laravel-package","media","sertxudeveloper"],"created_at":"2024-11-17T20:51:04.059Z","updated_at":"2024-11-17T20:51:04.819Z","avatar_url":"https://github.com/sertxudeveloper.png","language":"PHP","funding_links":["https://github.com/sponsors/sertxudev","https://buymeacoffee.com/sertxudeveloper"],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\u003cimg src=\"/art/socialcard.png\" alt=\"Laravel Media Model by Sertxu Developer\"\u003e\u003c/p\u003e\n\n# Attach media files to your models\n\n![](https://img.shields.io/github/v/release/sertxudeveloper/laravel-media-model) ![](https://github.com/sertxudeveloper/laravel-media-model/actions/workflows/run-tests.yml/badge.svg) ![](https://img.shields.io/github/license/sertxudeveloper/laravel-media-model) ![](https://img.shields.io/github/repo-size/sertxudeveloper/laravel-media-model) ![](https://img.shields.io/packagist/dt/sertxudeveloper/laravel-media-model) ![](https://img.shields.io/github/issues/sertxudeveloper/laravel-media-model) ![](https://img.shields.io/packagist/php-v/sertxudeveloper/laravel-media-model) [![Codecov Test coverage](https://img.shields.io/codecov/c/github/sertxudeveloper/laravel-media-model)](https://app.codecov.io/gh/sertxudeveloper/laravel-media-model)\n\nWhen developing an app, you may want to attach media files to your models, such as images, videos, or documents.\nWith this package, you can easily attach media files to your models.\n\nThe media files are stored in the specified disk and are related to the model using your database.\u003cbr\u003e\n\nYou can link local media files or remote ones without needing to download them to your server.\n\nThe main difference between this package and the other ones available is that this package allows you to relate media files to your models using a custom table per model.\n\nAllowing you to manage the media tables per model instead of one unique media table for all models.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require sertxudeveloper/laravel-media-model\n```\n\nNext, you should publish the config file and the migrations:\n\n```bash\nphp artisan vendor:publish --provider \"SertxuDeveloper\\Media\\MediaServiceProvider\"\n```\n\nAfter that, you can run the new migrations:\n\n```bash\nphp artisan migrate\n```\n\nFinally, you can start using the package!\n\n## Usage\n\nYou should modify the model that you want to attach media files to.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse SertxuDeveloper\\Media\\HasMedia;\nuse SertxuDeveloper\\Media\\Interfaces\\MediaInteraction;\n\nclass Message extends Model implements MediaInteraction\n{\n    use HasMedia;\n}\n```\n\nAs you can see, the model has been modified to use the `HasMedia` trait.\nAlso, the model must implement the `MediaInteraction` interface.\n\n### Separated media tables\n\nIf you want to use a separated media table for each model, you should modify the model that you want to attach media files to.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse SertxuDeveloper\\Media\\HasMedia;\nuse SertxuDeveloper\\Media\\Interfaces\\MediaInteraction;\n\nclass Message extends Model implements MediaInteraction\n{\n    use HasMedia;\n\n    public function getMediaTable(): string\n    {\n        return 'messages_media';\n    }\n}\n```\n\nThe specified table needs to be created, you can create it manually or use one of the following command.\n\n```bash\nphp artisan media:create-table messages_media\n```\n\nor\n\n```bash\nphp artisan media:create-table messages\n```\n\nor\n\n```bash\nphp artisan media:create-table \"App\\Models\\Message\"\n```\n\n### Attaching media files\n\nOnce you configured the model, you can attach media files to it. For example:\n\n\u003e **Note**\u003cbr\u003e\n\u003e If you don't specify a disk, the default disk will be used.\n\n```php\n\u003c?php\n\n$message = Message::find(1);\n\n$message-\u003eaddMediaFromDisk(path: '/images/image.jpg', disk: 'public');\n```\n\nYou can also attach a remote file:\n\n\u003e **Note**\u003cbr\u003e\n\u003e This will not download the file to your server. It will only add the remote file path to the database.\n\n```php\n\u003c?php\n\n$message = Message::find(1);\n\n$message-\u003eaddMediaFromUrl('https://www.sertxudeveloper.com/assets/logo.svg');\n```\n\nAlso, you can attach a file content, this will save the file to the disk and attach it to the model.\n\n\u003e **Note**\u003cbr\u003e\n\u003e This is useful if you get the content of a file from an external source, like as an email attachment read by a mail parser.\n\n```php\n\u003c?php\n\n$message = Message::find(1);\n\n$message-\u003eaddMediaFromContent(\n  content: file_get_contents('/tmp/tmpA3ds2'),\n  originalName: 'image.jpg',\n  toFolder: 'avatars',\n  toDisk: 'public'\n);\n```\n\n## Testing\n\nThis package contains tests, you can run them using the following command:\n\n```bash\ncomposer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/sertxudeveloper/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Sergio Peris](https://github.com/sertxudev)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n\u003cbr\u003e\u003cbr\u003e\n\u003cp align=\"center\"\u003eCopyright © 2022 Sertxu Developer\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsertxudeveloper%2Flaravel-media-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsertxudeveloper%2Flaravel-media-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsertxudeveloper%2Flaravel-media-model/lists"}