{"id":13828239,"url":"https://github.com/optixsolutions/laravel-media","last_synced_at":"2026-04-08T20:53:53.626Z","repository":{"id":32850468,"uuid":"135501316","full_name":"optixsolutions/laravel-media","owner":"optixsolutions","description":"Attach files to eloquent models","archived":false,"fork":false,"pushed_at":"2023-11-28T15:52:32.000Z","size":214,"stargazers_count":339,"open_issues_count":11,"forks_count":39,"subscribers_count":8,"default_branch":"master","last_synced_at":"2026-04-08T20:53:44.412Z","etag":null,"topics":["laravel","media-manager","php"],"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/optixsolutions.png","metadata":{"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}},"created_at":"2018-05-30T21:53:37.000Z","updated_at":"2026-02-03T23:53:27.000Z","dependencies_parsed_at":"2024-01-18T05:20:34.263Z","dependency_job_id":"0af58b89-b3b1-4112-baea-41c50b4fa05a","html_url":"https://github.com/optixsolutions/laravel-media","commit_stats":{"total_commits":113,"total_committers":5,"mean_commits":22.6,"dds":0.5663716814159292,"last_synced_commit":"f1935c3982a6943429912a144d51d113198420fb"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/optixsolutions/laravel-media","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/optixsolutions%2Flaravel-media","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/optixsolutions%2Flaravel-media/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/optixsolutions%2Flaravel-media/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/optixsolutions%2Flaravel-media/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/optixsolutions","download_url":"https://codeload.github.com/optixsolutions/laravel-media/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/optixsolutions%2Flaravel-media/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31573788,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["laravel","media-manager","php"],"created_at":"2024-08-04T09:02:37.944Z","updated_at":"2026-04-08T20:53:53.600Z","avatar_url":"https://github.com/optixsolutions.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Media\n\nAn easy solution to attach files to your eloquent models, with image manipulation built in!\n\n[![Packagist Version](https://img.shields.io/packagist/v/optix/media.svg)](https://packagist.org/packages/optix/media)\n[![Build Status](https://travis-ci.org/optixsolutions/laravel-media.svg?branch=master)](https://travis-ci.org/optixsolutions/laravel-media)\n[![License](https://img.shields.io/github/license/optixsolutions/laravel-media.svg)](https://github.com/optixsolutions/laravel-media/blob/master/LICENSE.md)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require optix/media\n```\n\nOnce installed, you should publish the provided assets to create the necessary migration and config files.\n\n```bash\nphp artisan vendor:publish --provider=\"Optix\\Media\\MediaServiceProvider\"\n```\n\n## Key concepts\n\nThere are a few key concepts that should be understood before continuing:\n\n* Media can be any type of file, from a jpeg to a zip file. You should specify any file restrictions in your\n  application's validation logic before you attempt to upload a file.\n\n* Media is uploaded as its own entity. It does not belong to another model in the system when it's created, so items can\n  be managed independently (which makes it the perfect engine for a media manager).\n  \n* Media must be attached to a model for an association to be made.\n\n* Media items are bound to \"groups\". This makes it easy to associate multiple types of media to a model. For\n  example, a model might have an \"images\" group and a \"documents\" group.\n  \n* You can manipulate images using conversions. You can specify conversions to be performed when a media item is\n  associated to a model. For example, you can register a \"thumbnail\" conversion to run when images are attached to a\n  model's \"gallery\" group.\n\n* Conversions are registered globally. This means that they can be reused across your application, i.e a Post and a\n  User can have the same sized thumbnail without having to register the same conversion twice.\n\n## Usage\n\n### Upload media\n\nYou should use the `Optix\\Media\\MediaUploader` class to handle file uploads.\n\nBy default, this class will update files to the disk specified in the media config. It saves them as a sanitised\nversion of their original file name, and creates a media record in the database with the file's details.\n\nYou can also customise certain properties of the file before it's uploaded.\n\n```php\n$file = $request-\u003efile('file');\n\n// Default usage\n$media = MediaUploader::fromFile($file)-\u003eupload();\n\n// Custom usage\n$media = MediaUploader::fromFile($file)\n    -\u003euseFileName('custom-file-name.jpeg')\n    -\u003euseName('Custom media name')\n    -\u003eupload();\n```\n\n### Associate media with a model\n\nIn order to associate a media item with a model, you must first include the `Optix\\Media\\HasMedia` trait.\n\n```php\nclass Post extends Model\n{\n    use HasMedia;\n}\n```\n\nThis trait will setup the relationship between your model and the media model. It's primary purpose is to provide a\nfluent api for attaching and retrieving media.\n\nOnce included, you can attach media to the model as demonstrated below. The first parameter of the attach media method\ncan either be a media model instance, an id, or an iterable list of models / ids.\n\n```php\n$post = Post::first();\n\n// To the default group\n$post-\u003eattachMedia($media);\n\n// To a custom group\n$post-\u003eattachMedia($media, 'custom-group');\n```\n\n### Disassociate media from a model\n\nTo disassociate media from a model, you should call the provided `detachMedia` method.\n\n```php\n// Detach all the media\n$post-\u003edetachMedia();\n\n// Detach the specified media\n$post-\u003edetachMedia($media);\n\n// Detach all the media in a group\n$post-\u003eclearMediaGroup('your-group');\n``` \n\nIf you want to delete a media item, you should do it the same way you would for any other model in your application.\n\n```php\nMedia::first()-\u003edelete();\n```\n\nDoing so will delete the file from your filesystem, and also remove any association between the media item and your\napplication's models.\n\n### Retrieve media\n\nAnother feature of the `HasMedia` trait is the ability to retrieve media.\n\n```php\n// All media in the default group\n$post-\u003egetMedia();\n\n// All media in a custom group\n$post-\u003egetMedia('custom-group');\n\n// First media item in the default group \n$post-\u003egetFirstMedia();\n\n// First media item in a custom group\n$post-\u003egetFirstMedia('custom-group');\n```\n\nAs well as retrieve media items, you can also retrieve attributes of the media model directly from your model.\n\n```php\n// Url of the first media item in the default group\n$post-\u003egetFirstMediaUrl();\n\n// Url of the first media item in a custom group\n$post-\u003egetFirstMediaUrl('custom-group');\n```\n\n### Manipulate Images\n\nThis package provides a fluent api to manipulate images. You can specify a model to perform \"conversions\" when\nmedia is attached to a group. It uses the familiar `intervention/image` library under the hood, so images can be\nmanipulated using all of the library's provided options.\n\nTo get started, you should first register a conversion in one of your application's service providers:\n\n```php\nuse Intervention\\Image\\Image;\nuse Optix\\Media\\Facades\\Conversion;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot()\n    {\n        Conversion::register('thumb', function (Image $image) {\n            return $image-\u003efit(64, 64);\n        });\n    }\n}\n```\n\nOnce you've registered a conversion, you should configure a media group to perform the conversion when media is\nattached to your model.\n\n```php\nclass Post extends Model\n{\n    use HasMedia;\n    \n    public function registerMediaGroups()\n    {\n        $this-\u003eaddMediaGroup('gallery')\n             -\u003eperformConversions('thumb');\n    }\n}\n```\n\nNow when a media item is attached to the \"gallery\" group, a converted image will be generated. You can get the url of\nthe converted image as demonstrated below:\n\n```php\n// The thumbnail of the first image in the gallery group\n$post-\u003egetFirstMediaUrl('gallery', 'thumb');\n```\n\n## Why use this package?\n\nThere are already packages that exist to solve a similar problem to the one that this package was built to achieve.\n\nThe most popular of which are:\n\n* [Spatie's Laravel MediaLibrary](https://github.com/spatie/laravel-medialibrary)\n* [Plank's Laravel Mediable](https://github.com/plank/laravel-mediable)\n\nThere are a few key differences between this package and the ones listed above. Our package was built to power media\nmanagers, and make it easy to perform image manipulations. This is better represented by the comparison table below:\n\n| Comparison                      | Spatie              | Plank        | Optix                |\n|---------------------------------|---------------------|--------------|----------------------|\n| **Relationship type**           | One to many         | Many to many | Many to many         |\n| **Provides image manipulation** | Yes                 | No           | Yes                  |\n| **Definition of manipulations** | Specific to a model | -            | Global registry      |\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foptixsolutions%2Flaravel-media","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foptixsolutions%2Flaravel-media","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foptixsolutions%2Flaravel-media/lists"}