{"id":21008387,"url":"https://github.com/binarcode/laravel-restable","last_synced_at":"2025-10-13T12:39:27.227Z","repository":{"id":44163802,"uuid":"353473882","full_name":"BinarCode/laravel-restable","owner":"BinarCode","description":"This is a lightweight REST API helper.","archived":false,"fork":false,"pushed_at":"2022-02-11T13:23:59.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-15T02:36:41.612Z","etag":null,"topics":[],"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/BinarCode.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":"BinarCode"}},"created_at":"2021-03-31T19:54:12.000Z","updated_at":"2022-10-20T15:27:20.000Z","dependencies_parsed_at":"2022-08-21T07:20:52.470Z","dependency_job_id":null,"html_url":"https://github.com/BinarCode/laravel-restable","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"spatie/package-skeleton-laravel","purl":"pkg:github/BinarCode/laravel-restable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinarCode%2Flaravel-restable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinarCode%2Flaravel-restable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinarCode%2Flaravel-restable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinarCode%2Flaravel-restable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BinarCode","download_url":"https://codeload.github.com/BinarCode/laravel-restable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinarCode%2Flaravel-restable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279015056,"owners_count":26085643,"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-13T02:00:06.723Z","response_time":61,"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":[],"created_at":"2024-11-19T09:12:24.684Z","updated_at":"2025-10-13T12:39:27.205Z","avatar_url":"https://github.com/BinarCode.png","language":"PHP","funding_links":["https://github.com/sponsors/BinarCode"],"categories":[],"sub_categories":[],"readme":"# Laravel Restable\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/binarcode/laravel-restable.svg?style=flat-square)](https://packagist.org/packages/binarcode/laravel-restable)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/binarcode/laravel-restable/run-tests?label=tests)](https://github.com/binarcode/laravel-restable/actions?query=workflow%3ATests+branch%3Amaster)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/binarcode/laravel-restable/Check%20\u0026%20fix%20styling?label=code%20style)](https://github.com/binarcode/laravel-restable/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/binarcode/laravel-restable.svg?style=flat-square)](https://packagist.org/packages/binarcode/laravel-restable)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require binarcode/laravel-restable\n```\n\n## Prerequisite\n\nTo associate search with a model, the model must implement the following interface and trait:\n\n```php\n\nnamespace App\\Models;\n\nuse BinarCode\\LaravelRestable\\HasRestable;\nuse BinarCode\\LaravelRestable\\Restable;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass YourModel extends Model implements Restable\n{\n    use HasRestable;\n}\n```\n\n## Usage\n\nUsing this lightweight package you can mainly customize `search`, `matches` and `sorts`.\n\n### Search\n\nDefine columns you want to be searchable using `$search` model property:\n\n```php\nclass Dream extends Model implements Restable\n{\n    use HasRestable;\n    \n    public static array $search = [\n        'name',\n    ];\n}\n```\n\nNow in the query of your request you can use the `?search=` to find over your model. Let's assume this is the URL for\ngeting the list of `dreams`:\n\n```http request\nGET: /api/dreams?search=be happy\n```\n\nThen in the controller you may have something like this:\n\n```php\nuse App\\Models\\Dream;\nuse Illuminate\\Http\\Request;\n\nclass DreamController extends Controller\n{\n    public function index(Request $request)\n    {\n        $dreams = Dream::search($request);\n        \n        return response()-\u003ejson($dreams-\u003epaginate());\n    }\n}\n```\n\nThis way Restable will find your `dreams` by `name` column and will return a `Builder` instance, so you can `paginate`\nor do whatever you want over that query.\n\nThe query filtering is something like this: `$query-\u003ewhere('column_name', 'like', \"%$value%\"`;\n\n## Match\n\nMatching by a specific column is a more strict type of search. You should define the columns you want to match along\nwith the type:\n\n```php\nuse BinarCode\\LaravelRestable\\Types;\n\nclass Dream extends Model implements Restable\n{\n    use HasRestable;\n    \n    public static array $match = [\n        'id' =\u003e Types::MATCH_ARRAY,\n        'name' =\u003e Types::MATCH_TEXT,\n    ];\n}\n```\n\nThe URL may look like this:\n\n```http request\nGET: /api/dreams?id=1,2,3\u0026name=happy\n```\n\nSo Restable will make a query like this:\n\n```php\n$query-\u003ewhereIn('id', [1, 2, 3])-\u003ewhere('name','=', 'happy');\n```\n\nThe controller could be same as we had for the search.\n\n### Sort\n\nYou can also specify what columns could be sortable:\n\n```php\nclass Dream extends Model implements Restable\n{\n    use HasRestable;\n    \n    public static array $sort = [\n        'id',\n        'name',\n    ];\n}\n```\n\nThe query params for sort could indicate whatever is `asc` or `desc` sorting by using the `-` sign:\n\nSorting `desc` by `id` column:\n\n```http request\nGET: /api/dreams?sort=-id\n```\n\nSorting `asc` by `id` column:\n\n```http request\nGET: /api/dreams?sort=id\n```\n\n## Customizations\n\n### Model Methods\n\nYou can use methods to return your `search`, `matches` or `sorts` from the model definition:\n\n```php\nclass Dream extends Model implements Restable\n{\n    use HasRestable;\n    \n    public static function sorts(): array\n    {\n        return [ 'id', 'name' ];\n    }\n\n    public static function matches(): array\n    {\n        return [\n            'id' =\u003e 'int',\n            'name' =\u003e 'string',\n        ];\n    }\n\n    public static function searchables(): array\n    {\n        return ['name'];\n    }\n}\n```\n\n### Custom filters\n\nInstead of using the default methods for filtering, you can have your own:\n\n```php\nuse BinarCode\\LaravelRestable\\Filters\\MatchFilter;class Dream extends Model implements Restable\n{\n    use HasRestable;\n\n    public static function matches(): array\n    {\n        return [\n            'something' =\u003e MatchFilter::make()-\u003eresolveUsing(function($request, $query) {\n                   // filter it here\n            }),\n            'name' =\u003e 'string'\n        ];\n    }\n}\n```\n\nSo you can now match by `something` property, and implement your own search into the closure. \n\nYou can also create your own `Match` filter class, and implement the search there:\n\n```php\nuse BinarCode\\LaravelRestable\\Filters\\MatchFilter;\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Illuminate\\Http\\Request;\n\nclass MatchSomething extends MatchFilter\n{\n        public function apply(Request $request, Builder $query, $value): Builder\n        {\n            // your filters\n            return $query-\u003ewhere('a', $value);\n        }\n\n}\n```\n\nThen use your `MatchSomething` filter:\n\n\n```php\nclass Dream extends Model implements Restable\n{\n    use HasRestable;\n\n    public static function matches(): array\n    {\n        return [\n            'something' =\u003e MatchSomething::make(),\n            'name' =\u003e 'string'\n        ];\n    }\n}\n```\n\nThe same you could do for `Search` or `Sort` filter, by extending the `BinarCode\\LaravelRestable\\Filters\\SearchableFilter` or `BinarCode\\LaravelRestable\\Filters\\SortableFilter` filters.\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## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Eduard Lupacescu](https://github.com/BinarCode)\n- [All Contributors](../../contributors)\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%2Fbinarcode%2Flaravel-restable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinarcode%2Flaravel-restable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarcode%2Flaravel-restable/lists"}