{"id":26535466,"url":"https://github.com/thiagoprz/crud-tools","last_synced_at":"2025-03-21T20:36:09.631Z","repository":{"id":34996028,"uuid":"194936969","full_name":"thiagoprz/crud-tools","owner":"thiagoprz","description":"Easy to use Laravel CRUD package with Controller, Model and Log system built in","archived":false,"fork":false,"pushed_at":"2023-10-24T01:01:01.000Z","size":516,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-02T20:08:39.091Z","etag":null,"topics":["crud","crud-controller","helpers-library","laravel","laravel-crud","php","traits"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/thiagoprz/crud-tools","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/thiagoprz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"License.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-02T21:25:46.000Z","updated_at":"2024-09-21T00:22:39.000Z","dependencies_parsed_at":"2023-02-12T04:16:53.605Z","dependency_job_id":null,"html_url":"https://github.com/thiagoprz/crud-tools","commit_stats":null,"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagoprz%2Fcrud-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagoprz%2Fcrud-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagoprz%2Fcrud-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagoprz%2Fcrud-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagoprz","download_url":"https://codeload.github.com/thiagoprz/crud-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244866809,"owners_count":20523576,"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":["crud","crud-controller","helpers-library","laravel","laravel-crud","php","traits"],"created_at":"2025-03-21T20:36:08.784Z","updated_at":"2025-03-21T20:36:09.601Z","avatar_url":"https://github.com/thiagoprz.png","language":"PHP","funding_links":["https://ko-fi.com/S6S4273NJ","https://www.buymeacoffee.com/thiagoprz"],"categories":[],"sub_categories":[],"readme":"# Laravel Crud Tools\nEasy to use Laravel CRUD package with Controller, Model and Log system built in.\n\n\n[![Documentation Status](https://readthedocs.org/projects/laravel-crud-tools/badge/?version=latest)](https://laravel-crud-tools.readthedocs.io/en/latest/?badge=latest)\n[![Dev](https://github.com/thiagoprz/crud-tools/actions/workflows/dev.yml/badge.svg?branch=dev)](https://github.com/thiagoprz/crud-tools/actions/workflows/dev.yml)\n[![Master](https://github.com/thiagoprz/crud-tools/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/thiagoprz/crud-tools/actions/workflows/master.yml)\n\n## Table of contents\n* [Installation](#installation)\n* [Usage](#usage)  \n  - [CRUD Model](#crud-model)\n  - [CRUD Controller](#crud-controller)\n* [CRUD Generators](#crud-generators)\n  - [Model Generator](#model-generator)\n  - [Controller Generator](#controller-generator)\n* [Enabling Logs](#enabling-logs)\n* [Customizing Routes and Resource Paths](#customizing-routes-and-resource-paths)\n* [Contributing](#contributing)\n* [Support](#support)\n\n## Installation\nInstall through composer using: ``composer install thiagoprz/crud-tools``\n\nIf you don't have package auto discovery enabled add CrudToolsServiceProvider to your `config/app.php`:\n\n```\n... \n'providers' =\u003e [\n    ...\n    \\Thiagoprz\\CrudTools\\CrudToolsServiceProvider::class,\n],\n...\n```\n\nPublish Crud Tools service provider to allow stubs customization:\n\n`` php artisan vendor:publish --provider=\"Thiagoprz\\CrudTools\\CrudToolsServiceProvider\"``\n\n\n\n## Usage\n\n\n### CRUD Model:\n\nFor models you just need to add the trait ModelCrud and after that create a static property declaring model's validations (based on the create, update and/or delete scenarios), default order, filtering rules, upload file rules, define resources, and with / countable relationships.\n\n\n- Validations:\n```\n\u003c?php\n...\nuse Thiagoprz\\CrudTools\\Models\\ModelCrud;\nuse Thiagoprz\\CrudTools\\Interfaces\\ModelCrudInterface;\nclass User extends Authenticatable implements ModelCrudInterface\n{\n    use ModelCrud;\n    \n    /**\n     * Model validations\n     *\n     * @var array\n     */\n    static $validations = [\n        'create' =\u003e [\n            'name' =\u003e ['required', 'string', 'max:255'],\n            'email' =\u003e ['required', 'string', 'email', 'max:255', 'unique:users'],\n            'password' =\u003e ['required', 'string', 'min:8', 'confirmed'],\n        ],\n        'update' =\u003e [\n            'name' =\u003e ['required', 'string', 'max:255'],\n            'email' =\u003e ['required', 'string', 'email', 'max:255', 'unique:users'],\n            'password' =\u003e ['required', 'string', 'min:8', 'confirmed'],\n        ],\n    ];\n    ...\n}\n```\n- Searchable fields:\n\nYou can create a $searchable property that will hold fields allowed to be searched on the static method **search()** - very useful with the ControllerCrud.\n\n```\n\u003c?php\n...\nuse Thiagoprz\\CrudTools\\Models\\ModelCrud;\nclass User extends Authenticatable\n{\n    use ModelCrud;\n    /**\n     * Fields that can be searched by (static)method search()\n     *\n     * @var array\n     */\n    static $searchable = [\n        'id' =\u003e 'int',\n        'name' =\u003e 'string',\n        'created_at' =\u003e 'datetime',\n    ];\n    ...\n}\n```\n\n- Range searchable fields:\n\nTypes available: int, string, date, datetime and decimal.\n\nYou can use input filters using \"_from\" and \"_to\" suffix on date, datetime and decimal fields:\n\n``` \n\u003c!-- Filtering created_at usig field \"from\" ( where created_at \u003e= $created_at_from ) --\u003e\n\u003clabel\u003ePeriod from: \u003c/label\u003e\n\u003cinput type=\"date\" name=\"created_at_from\"\u003e\n\n\u003c!-- Filtering created_at usig field \"to\" ( where created_at \u003c= $created_at_to ) --\u003e\n\u003clabel\u003eTo:\u003c/label\u003e\n\u003cinput type=\"date\" name=\"created_at_to\"\u003e\n```\n\n\n| Type      | Description         | Suffixes: _from _to | \n| --------- | ------------------- | ------------------- |\n|  int      | Integer fields, can be used to search a range of records by using \"_from\" and \"_to\"  suffixes | Yes |\n| decimal   | Float, Double, Real or any decimal type of field.  \"_from\" and \"_to\"  suffixes allowed | Yes |\n| string    | Any string field to be search using \"WHERE field LIKE '%SEARCH%'\" | No |\n| string    | Any string field to be search using \"WHERE field = 'SEARCH'\" | No |\n| datetime  | Datetime and Timestamp fields | Yes |\n| date      | Date fields | Yes |\n\n\n- Custom searchable field methods:\n\nIn addition to use standard search based on type of fields you can add your on custom methods to customize search of specific fields. Create a method called \"**searchField**\" where Field is the name of the field with only first letter upper case.\n\nExample:\n\n```\n\u003c?php\n...\nuse Thiagoprz\\CrudTools\\Models\\ModelCrud;\nclass Books extends Model\n{\n    ...\n    \n    /**\n     * Searching only by the start of the title of the book with LIKE\n     */\n    public static function searchTitle($query, $title)\n    {\n        $query-\u003ewhere('title', 'LIKE', \"$title%\");    \n    }\n\n}\n\n\n```\n\n- Sortable fields:\n\nYou can define the fields that will be used as default sorting of your model on the index action. Also, you can pass an \"order\" input used by the search method allowing the override the default order defined by this variable.\n\n```\n\u003c?php\n...\nuse Thiagoprz\\CrudTools\\Models\\ModelCrud;\nuse Thiagoprz\\CrudTools\\Interfaces\\ModelCrudInterface;\nclass Books extends Model implements ModelCrudInterface\n{\n    use ModelCrud;\n    /**\n     * Default order\n     *\n     * @var array\n     */\n    static $search_order = [\n        'title' =\u003e 'ASC',\n        'updated_at' =\u003e 'DESC',\n        'created_at' =\u003e 'DESC',\n    ];\n    ...\n}\n```\n\n\n- Upload fields:\n\nYou can create a fileUploads method to define which and where your uploadable fields will store the files:\n\n```\n\u003c?php\n...\nuse Thiagoprz\\CrudTools\\Models\\ModelCrud;\nuse Thiagoprz\\CrudTools\\Interfaces\\ModelCrudInterface;\nclass User extends Authenticatable implements ModelCrudInterface\n{\n    use ModelCrud;\n    ...\n    /**\n     * @param Campaign $model\n     * @return array\n     */\n    public static function fileUploads(Campaign $model)\n    {\n        return [\n            'FIELD_NAME' =\u003e [\n                'path' =\u003e 'FOLDER', // Mandatory\n                'name' =\u003e 'FILE_NAME', // (OPTIONAL)if not provided will be the file original name \n            ],\n        ];\n    }\n    ...\n}\n```\n\n\n\n### CRUD Controller:\nA CRUD Controller can be achieve by just creating a standard controller class using ControllerCrud trait.\n\nThe next step is to create a folder inside ``resources/views`` with the desired namespace or on root folder if the controller won't be using a specific namespace (admin on the example).\n```\n\u003c?php\n\nnamespace App\\Http\\Controllers\\Admin;\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Models\\User;\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Foundation\\Validation\\ValidatesRequests;\nuse Thiagoprz\\CrudTools\\Http\\Controllers\\ControllerCrud;\nuse Thiagoprz\\CrudTools\\Interfaces\\ControllerCrudInterface;\n\nclass UserController extends Controller implements ControllerCrudInterface\n{\n    use ValidatesRequests;\n    use ControllerCrud;\n    public $modelClass = User::class;\n}\n```\n\nViews directory structure used by Controller CRUD based on the above example:\n\nFolder: \n\u003e views/admin/user\n\nFiles:\n\u003e create.blade.php\n\n\u003e edit.blade.php \n\nAvailable vars: $model (the model being updated) \n\n\u003e form.blade.php\n\nAvailable vars: $model (the model being updated - only on edit action)\n\n\u003e index.blade.php\n\nAvailable vars: $items (the pagination object containing a filtered collection of the model)\n\n\u003e show.blade.php\n\nAvailable vars: $model (the model being displayed)\n\n## CRUD Generators\n\n### Model Generator:\nTo easily create a model with all Crud Tools enabled use:\n```\nphp artisan make:crud-model NAMESPACE/Model   \n```\n\u003e NAMESPACE: Model's namespace\n\u003e Model: Name of the model\n\n- Available options\n  - **--fillable**: comma separated fields for fillable attributes\n  - **--searchable**: comma separated fields for searchable attributes (based on search() method)\n  - **--primaryKey**: field or comma separated fields that are the table's primary key\n  - **--softDeletes**: if passed enables SoftDeletes trait on class\n  - **--uploads**: if passed adds fileUploads() method on class \n  - **--logable**: adds Logable trait on model\n\n### Controller Generator:\n\nYou can create a standard Controller to work with a model by using the following command:\n\n``` php artisan make:crud-controller NAMESPACE1/NAMEController NAMESPACE2/Model ```\n\n\u003e NAMESPACE1: Controller's namespace\n\u003e\n\u003e NAMEController: is the name of the controller\n\u003e\n\u003e NAMESPACE2: Model's namespace\n\u003e\n\u003e Model: Name of the model\n\n## Enabling Logs\nTo enable automatic logs on your models you need to publish Spatie Activity Logger migrations:\n\n``php artisan vendor:publish --provider=\"Spatie\\Activitylog\\ActivitylogServiceProvider\" --tag=\"migrations\"``\n\nRun migrations:\n\n``php artisan migrate``\n\nFor more information you can read Spatie Activity Log [Documentations](https://github.com/spatie/laravel-activitylog).\n\n\n## Customizing Routes and Resource Paths\nIf you need to have different structure for resource directories besides the default `resources/{namespace/}modelname/` you can implement the method getViewPath on your controller and define it up manually: \n\n```\n    /**\n     * @param $forRedirect\n     * @return string\n     */\n    public function getViewPath($forRedirect = false): string\n    {\n        return $forRedirect ? 'custom/url' : 'custom.path';\n    }\n```\nThis method returns the path for routes and views, so you can customize the url and path for resources separatelly just using `$forRedirect` `true` for routes and `false` for resource directories.\n\n\n## Contributing\nCheck the [contributing](CONTRIBUTING.md) file to have a better understanding on how to contribute to the package.\n\n## Support\n\n### Issues\nPlease feel free to indicate any issues on this packages, it will help a lot. I will address it as soon as possible.\n\n### Supported By Jetbrains\nThis project is being developed with the help of [Jetbrains](https://www.jetbrains.com/?from=LaravelCrudTools) through its project to support Open Source software.\n\n![Test Image 1](support/jetbrains.svg)\n\n### Buy me a Coffee\n[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/S6S4273NJ)\n[![buy-coffee](https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-1.svg)](https://www.buymeacoffee.com/thiagoprz)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagoprz%2Fcrud-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagoprz%2Fcrud-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagoprz%2Fcrud-tools/lists"}