{"id":21168598,"url":"https://github.com/laratipsofficial/save-model","last_synced_at":"2025-10-15T12:16:04.784Z","repository":{"id":47141988,"uuid":"384649915","full_name":"Laratipsofficial/save-model","owner":"Laratipsofficial","description":"Save Model is a Laravel package that allows you to save data in the database in a new way. I have also made videos on YouTube about this concept. Check it out: https://www.youtube.com/c/laratips","archived":false,"fork":false,"pushed_at":"2021-09-12T05:02:01.000Z","size":54,"stargazers_count":28,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-09T19:49:04.471Z","etag":null,"topics":["laravel","laravel-package","php","php8"],"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/Laratipsofficial.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"asdh"}},"created_at":"2021-07-10T08:31:22.000Z","updated_at":"2023-04-25T19:08:33.000Z","dependencies_parsed_at":"2022-07-21T13:18:12.907Z","dependency_job_id":null,"html_url":"https://github.com/Laratipsofficial/save-model","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":"spatie/package-skeleton-laravel","purl":"pkg:github/Laratipsofficial/save-model","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Laratipsofficial%2Fsave-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Laratipsofficial%2Fsave-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Laratipsofficial%2Fsave-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Laratipsofficial%2Fsave-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Laratipsofficial","download_url":"https://codeload.github.com/Laratipsofficial/save-model/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Laratipsofficial%2Fsave-model/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279077248,"owners_count":26098234,"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","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","laravel-package","php","php8"],"created_at":"2024-11-20T15:16:11.719Z","updated_at":"2025-10-15T12:16:04.764Z","avatar_url":"https://github.com/Laratipsofficial.png","language":"PHP","funding_links":["https://github.com/sponsors/asdh"],"categories":[],"sub_categories":[],"readme":"# Save Model\n\nSave Model is a Laravel package that allows you to save data in the database in a new way. No need to worry about `$guarded` and `$fillable` properties in the model anymore. Just relax an use `Save Model` package.\n\n---\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/asdh/save-model.svg?style=flat-square)](https://packagist.org/packages/asdh/save-model)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/asdh/save-model/run-tests?label=tests)](https://github.com/asdh/save-model/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/asdh/save-model/Check%20\u0026%20fix%20styling?label=code%20style)](https://github.com/asdh/save-model/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/asdh/save-model.svg?style=flat-square)](https://packagist.org/packages/asdh/save-model)\n\n---\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require asdh/save-model\n```\n\nYou can publish the config file with:\n```bash\nphp artisan save-model:publish\n\nor\n\nphp artisan vendor:publish --provider=\"Asdh\\SaveModel\\SaveModelServiceProvider\"\n```\n\nThis is the contents of the published config file:\n\n```php\n// config/save_model.php\n\nreturn [\n    /**\n     * The directory name where the files should be stored\n     * This can be changed via 'saveableFields' method on model\n     */\n    'file_upload_directory' =\u003e 'files',\n];\n```\n\n## Usage\n\n```php\n// In controller\n\nuse Asdh\\SaveModel\\SaveModel;\n\nSaveModel::new(\n    new User,\n    $request-\u003eonly(['name', 'email', 'password', 'image'])\n)-\u003eexecute();\n\n// OR\n\n(new SaveModel(\n    new User,\n    $request-\u003eonly(['name', 'email', 'password', 'image'])\n)-\u003eexecute();\n```\n\nYou just do this and a new user will be created and saved to the 'users' table. The password will be automatically hashed, and uploading of the image will also be automatically handled.\n\nTo update a model, you just have to pass the model that you want to update.\n\n```php\n// In controller\n\nuse Asdh\\SaveModel\\SaveModel;\n\n$user = User::find(1);\n\nSaveModel::new(\n    $user,\n    $request-\u003eonly(['name', 'email'])\n)-\u003eexecute();\n```\nOnly name and email will be updated and no other columns will be touched.\n\n**For this to work, you need to do these things:**\n\nGo to User model class or any other model class and add `CanBeSavedContract` class to it. In this example, I will use User model.\n\n```php\nuse Asdh\\SaveModel\\Contracts\\CanBeSavedContract;\n\nclass User extends Authenticatable implements CanBeSavedContract\n{\n\n}\n```\n\nAfter adding this, you need to add `saveableFields` method to the User model and map every columns of the users table like so:\n\n```php\nuse Asdh\\SaveModel\\Contracts\\CanBeSavedContract;\nuse Asdh\\SaveModel\\Fields\\DatetimeField;\nuse Asdh\\SaveModel\\Fields\\FileField;\nuse Asdh\\SaveModel\\Fields\\PasswordField;\nuse Asdh\\SaveModel\\Fields\\StringField;\n\nclass User extends Authenticatable implements CanBeSavedContract\n{\n    public function saveableFields(): array\n    {\n        return [\n            'name' =\u003e StringField::new(),\n            'email' =\u003e StringField::new(),\n            'email_verified_at' =\u003e DatetimeField::new(),\n            'password' =\u003e PasswordField::new(),\n            'image' =\u003e FileField::new(),\n        ];\n    }\n}\n```\n\nAfter doing this you are good to go. In the controller, you just need to get the data and use the `SaveModel` class\n\n```php\nuse Asdh\\SaveModel\\SaveModel;\n\nSaveModel::new(\n    new User,\n    $request-\u003eonly(['name', 'email', 'password', 'image'])\n)-\u003eexecute();\n\n// OR\n\n(new SaveModel(\n    new User,\n    $request-\u003eonly(['name', 'email', 'password', 'image'])\n)-\u003eexecute();\n```\n\nThe files will be uploaded using the default `Laravel's filesystem`. Which means that you can directly configure to upload the files directly to the `S3` as well or any other that Laravel supports.\n\nAlso, the files will be uploaded to the `files` directory by default. You can change that globally by changing the value of `file_upload_directory` on the `save_model.php` configuration file.\n\nYou can also change it per model like so:\n\n```php\n// app/Models/User.php\n\npublic function saveableFields(): array\n{\n    return [\n        'image' =\u003e FileField::new()-\u003esetDirectory('images'),\n    ];\n}\n```\n\nIt will now store the `image` of the user to the `images` directory and for every other `Models`, it will use from the `save_model.php` config file.\n\nYou can also, choose the Laravel Filesystem's `disk` per model like so:\n\n```php\n// app/Models/User.php\n\npublic function saveableFields(): array\n{\n    return [\n        'image' =\u003e FileField::new()\n            -\u003esetDirectory('images')\n            -\u003esetDisk('s3'),\n    ];\n}\n```\n\nBy default `random name` will be generated for the uploaded files, but you can change that also. You just have to pass closure on the `setFileName` method and you will get access to the uploaded file there. And whatever you return from here will be saved to the database as the file name.\n\nThis example shows how to return the original file name.\n\n```php\n// app/Models/User.php\n\nuse Illuminate\\Http\\UploadedFile;\n\npublic function saveableFields(): array\n{\n    return [\n        'image' =\u003e FileField::new()\n            -\u003esetDirectory('images')\n            -\u003esetFileName(function (UploadedFile $uploadedFile) {\n                return $uploadedFile-\u003egetClientOriginalName();\n            }),\n    ];\n}\n```\n\nIf you want to upload the file as the original name then you can do this:\n\n```php\n// app/Models/User.php\n\nuse Illuminate\\Http\\UploadedFile;\n\npublic function saveableFields(): array\n{\n    return [\n        'image' =\u003e FileField::new()\n            -\u003esetDirectory('images')\n            -\u003euploadAsOriginalName(),\n    ];\n}\n```\n\nOne thing to keep in mind that the `setFileName` method will take precedence over `uploadAsOriginalName` if both methods are being used.\n\nNot only this, the deletion of the file will also be automatically handled when updating a model. By default, when a model is updated, the old file will be automatically deleted if a new file is being uploaded. If you don't want the old images to be deleted then you can chain `dontDeleteOldFileOnUpdate` method.\n\n```php\n// app/Models/User.php\n\nuse Illuminate\\Http\\UploadedFile;\n\npublic function saveableFields(): array\n{\n    return [\n        'image' =\u003e FileField::new()\n            -\u003esetDirectory('images')\n            -\u003edontDeleteOldFileOnUpdate(),\n    ];\n}\n```\n\n## Available Fields\n```php\nAsdh\\SaveModel\\Fields\\StringField::class\nAsdh\\SaveModel\\Fields\\IntegerField::class\nAsdh\\SaveModel\\Fields\\DatetimeField::class\nAsdh\\SaveModel\\Fields\\DateField::class\nAsdh\\SaveModel\\Fields\\TimeField::class\nAsdh\\SaveModel\\Fields\\PasswordField::class\nAsdh\\SaveModel\\Fields\\FileField::class\nAsdh\\SaveModel\\Fields\\BooleanField::class\n```\n\nOther field will be added in the future and I am open to pull requests.\n\n## Creating your own model field class\n\nYou can create your own field class as well. To create one, you need to run an artisan command\n\n```bash\nphp artisan make:field BooleanField\n```\n\nThis will create a `BooleanField` class inside `App\\ModelFields` directory and it will look like this:\n\n```php\n\u003c?php\n\nnamespace App\\ModelFields;\n\nuse Asdh\\SaveModel\\Fields\\Field;\n\nclass BooleanField extends Field\n{\n    public function execute(): mixed\n    {\n        // Perform your logic and return the value...\n\n        // return strtoupper($this-\u003evalue)\n    }\n}\n```\n\nIt is not necessary that `BooleanField` class must be inside `App\\ModelFields` directory. You can place it wherever you like.\n\nYou will have access to the data passed from the controller as `$this-\u003evalue`. Then you can do whatever you want to do and return the value that you want to save in the database. In above case, we can do it like so:\n\n```php\n\u003c?php\n\nnamespace App\\ModelFields;\n\nuse Asdh\\SaveModel\\Fields\\Field;\n\nclass BooleanField extends Field\n{\n    public function execute(): mixed\n    {\n        return in_array($this-\u003evalue, [1, '1', true, 'true', 'on', 'yes']);\n    }\n}\n\n```\nIf the input is any one of these, then we will consider it to be true and for every one of these values, we will save `true` (which will be `1` when stored in database) to the database.\n\nThen you can easily use your own field in the model's `saveableFields` method. You can now use this `BooleanField` along with other fields like this:\n\n```php\nuse Asdh\\SaveModel\\Contracts\\CanBeSavedContract;\nuse Asdh\\SaveModel\\Fields\\DatetimeField;\nuse Asdh\\SaveModel\\Fields\\FileField;\nuse Asdh\\SaveModel\\Fields\\PasswordField;\nuse Asdh\\SaveModel\\Fields\\StringField;\nuse App\\ModelFields\\BooleanField;\n\nclass User extends Authenticatable implements CanBeSavedContract\n{\n    public function saveableFields(): array\n    {\n        return [\n            'name' =\u003e StringField::new(),\n            'email' =\u003e StringField::new(),\n            'email_verified_at' =\u003e DatetimeField::new(),\n            'password' =\u003e PasswordField::new(),\n            'image' =\u003e FileField::new(),\n            'is_admin' =\u003e Boolean::new(),\n        ];\n    }\n}\n\n```\nMake sure you add the namespace correctly as shown above.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\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%2Flaratipsofficial%2Fsave-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaratipsofficial%2Fsave-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaratipsofficial%2Fsave-model/lists"}