{"id":43295322,"url":"https://github.com/movor/laravel-custom-casts","last_synced_at":"2026-02-01T19:04:16.328Z","repository":{"id":57020245,"uuid":"144054841","full_name":"movor/laravel-custom-casts","owner":"movor","description":"Make your own cast type for Laravel model","archived":false,"fork":false,"pushed_at":"2018-12-05T15:18:44.000Z","size":20,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-30T23:00:03.963Z","etag":null,"topics":["accessor","attribute","cast","casting","custom","laravel","mutator"],"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/movor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-08T18:48:36.000Z","updated_at":"2020-12-15T01:22:31.000Z","dependencies_parsed_at":"2022-08-23T13:50:36.884Z","dependency_job_id":null,"html_url":"https://github.com/movor/laravel-custom-casts","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/movor/laravel-custom-casts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/movor%2Flaravel-custom-casts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/movor%2Flaravel-custom-casts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/movor%2Flaravel-custom-casts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/movor%2Flaravel-custom-casts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/movor","download_url":"https://codeload.github.com/movor/laravel-custom-casts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/movor%2Flaravel-custom-casts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28986443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T18:17:03.387Z","status":"ssl_error","status_checked_at":"2026-02-01T18:16:57.287Z","response_time":56,"last_error":"SSL_read: 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":["accessor","attribute","cast","casting","custom","laravel","mutator"],"created_at":"2026-02-01T19:04:16.134Z","updated_at":"2026-02-01T19:04:16.321Z","avatar_url":"https://github.com/movor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Custom Casts\n\n[![Build](https://api.travis-ci.org/movor/laravel-custom-casts.svg?branch=master)](https://travis-ci.org/movor/laravel-custom-casts)\n[![Downloads](https://poser.pugx.org/movor/laravel-custom-casts/downloads)](https://packagist.org/packages/movor/laravel-custom-casts)\n[![Stable](https://poser.pugx.org/movor/laravel-custom-casts/v/stable)](https://packagist.org/packages/movor/laravel-custom-casts)\n[![License](https://poser.pugx.org/movor/laravel-custom-casts/license)](https://packagist.org/packages/movor/laravel-custom-casts)\n\n### Make your own custom cast type for Laravel model attributes\n\nBy default, from version 5 Laravel supports attribute casting. If we define `$cast` property on our model, Laravel will\nhelp us convert defined attributes to common data types. Currently supported cast types (Laravel 5.6) are: `integer`,\n`real`, `float`, `double`, `string`, `boolean`, `object`, `array`, `collection`, `date`, `datetime` and `timestamp`.\n\nIf those default cast types are not enough and you want to make your own, you'r on the right track.\n\n---\n\n:heavy_exclamation_mark::heavy_exclamation_mark::heavy_exclamation_mark:\n**MOVING GIT REPOSITORY**\n:heavy_exclamation_mark::heavy_exclamation_mark::heavy_exclamation_mark:\n\nThis package **repo and maintenance will continue on**:\n\n[**vkovic/laravel-custom-casts**](https://github.com/vkovic/laravel-custom-casts).\n\n:truck:  :truck:  :truck:  :truck:  :truck:  :truck:  :truck:  :truck:\n:truck:  :truck:  :truck:  :truck:  :truck:  :truck:  :truck:\n\n---\n\n## Compatibility\n\nThe package is compatible with Laravel versions `\u003e= 5.1`\n\n## Installation\n\nInstall the package via composer:\n\n```bash\ncomposer require movor/laravel-custom-casts\n```\n\n## Example: Casting User Image\n\nWhen saving an image, there is two things that needs to be done:\n1. Save image name (sometimes with path) into corresponding database field\n2. Save image physically on the disk\n\nAs a guidance for this example we'll use default Laravel user model found in `app/User.php`.\n\nBeside basic, predefined fields: `name`, `email` and `password`, we also want to allow user to upload his avatar. Assume\nthat we already have `users` table with `image` field (you should create seeder for this).\n\nTo utilize custom casts, we'll need to add trait to user model, and via `$casts` property link it to the cast class.\n\n```php\n// File: app/User.php\n\nnamespace App;\n\nuse App\\CustomCasts\\ImageCast;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Illuminate\\Notifications\\Notifiable;\nuse Movor\\LaravelCustomCasts\\HasCustomCasts;\n\nclass User extends Authenticatable\n{\n    use Notifiable, HasCustomCasts;\n\n    protected $fillable = [\n        'name', 'email', 'password', 'image'\n    ];\n\n    protected $hidden = [\n        'password', 'remember_token'\n    ];\n\n    protected $casts = [\n        'image' =\u003e ImageCast::class\n    ];\n}\n\n// ...\n```\n\nNext step is to create class that'll handle casting. It must implement `setAttribute` method which will take care of\nsaving the image (from UploadedFile object, via form upload in this case) and generating image name with path - to be preserved in database.\n\n```php\n// File: app/CustomCasts/ImageCast.php\n\nnamespace App\\CustomCasts;\n\nuse Movor\\LaravelCustomCasts\\CustomCastBase;\nuse Illuminate\\Http\\UploadedFile;\n\nclass ImageCast extends CustomCastBase\n{\n    public function setAttribute($file)\n    {\n        // Define storage folder\n        // (relative to \"storage/app\" folder in Laravel project)\n        // Don't forget to create it !!!\n        $storageDir = 'images';\n\n        // Generate random image name\n        $filename = str_random() . '.' . $file-\u003eextension();\n\n        // Save image to predefined folder\n        $file-\u003estoreAs($storageDir, $filename);\n\n        // This will be stored in db field: \"image\"\n        return $storageDir . '/' . $filename;\n    }\n}\n```\n\nLet's jump to user creation example. This will trigger our custom cast logic.\n\nAssume that we have user controller which will handle user creation. You should create this on your\nown.\n\n\u003e Code below is just a simple example and should be used as guidance only.\n\n```php\n// File: app/Http/Controllers/UserController.php\n\n// ...\n\nprotected function create(Request $request)\n{\n    User::create([\n        'name' =\u003e $request-\u003ename,\n        'email' =\u003e $request-\u003eemail,\n        'password' =\u003e bcrypt($request-\u003epassword),\n        // Past the whole Illuminate\\Http\\UploadedFile object,\n        // we'll handle it in our ImageCast class\n        'image' =\u003e $request-\u003efile('image')\n    ]);\n}\n\n// ...\n```\n\nVisit corresponding route input basic details and attach the image. After that, we'll have our user created and image\nstored.\n\nBut we should also handle deleting image when user is deleted. This can be accomplished by utilizing underlying eloquent\nevents handling. Each time eloquent event is fired, logic will look up for public method with the same name in our custom\ncast class.\n\nPossible method names are:\n`retrieved`, `creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`, `restoring` and\n`restored`.\n\n```php\n// File: app/CustomCasts/ImageCast.php\n\n// Add at the top\nuse Storage;\n\n// ...\n\n// This method will be triggered after model has been deleted\npublic function deleted()\n{\n    // We can access underlying model with $this-\u003emodel\n    // and attribute name that is being casted with $this-\u003eattribute\n\n    // Retrieve image path and delete it from the disk\n    $imagePath = $this-\u003emodel-\u003eimage;\n    Storage::delete($imagePath);\n}\n\n// ...\n\n```\n\nThis should cover basics usage of custom casts.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmovor%2Flaravel-custom-casts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmovor%2Flaravel-custom-casts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmovor%2Flaravel-custom-casts/lists"}