{"id":36575350,"url":"https://github.com/jedrzej/withable","last_synced_at":"2026-01-12T07:29:48.124Z","repository":{"id":36489551,"uuid":"40795162","full_name":"jedrzej/withable","owner":"jedrzej","description":"Eager load Eloquent's relations with request parameters","archived":false,"fork":false,"pushed_at":"2022-10-15T16:35:33.000Z","size":13,"stargazers_count":24,"open_issues_count":4,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-26T20:57:18.104Z","etag":null,"topics":["eager-loading","eloquent","laravel","php"],"latest_commit_sha":null,"homepage":null,"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/jedrzej.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-16T01:36:30.000Z","updated_at":"2022-08-19T08:45:57.000Z","dependencies_parsed_at":"2022-09-06T11:00:31.079Z","dependency_job_id":null,"html_url":"https://github.com/jedrzej/withable","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/jedrzej/withable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzej%2Fwithable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzej%2Fwithable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzej%2Fwithable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzej%2Fwithable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedrzej","download_url":"https://codeload.github.com/jedrzej/withable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedrzej%2Fwithable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28336524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"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":["eager-loading","eloquent","laravel","php"],"created_at":"2026-01-12T07:29:48.067Z","updated_at":"2026-01-12T07:29:48.117Z","avatar_url":"https://github.com/jedrzej.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Withable trait for Laravel's Eloquent models\n\nThis package adds possibility to dynamically eager load Eloquent models relations in Laravel 4/5/6 using request parameters.\n\nYou could also find those packages useful:\n\n- [Searchable](https://github.com/jedrzej/searchable) - Allows filtering your models using request parameters\n- [Sortable](https://github.com/jedrzej/sortable) - Allows sorting your models using request parameters\n- [Pimpable](https://github.com/jedrzej/pimpable) - A meta package that combines Sortable, Searchable and Withable behaviours\n\n## Composer install\n\nAdd the following line to `composer.json` file in your project:\n\n    \"jedrzej/withable\": \"0.0.6\"\n\t\nor run the following in the commandline in your project's root folder:\t\n\n    composer require \"jedrzej/withable\" \"0.0.6\"\n\n## Setting up withable models\n\nIn order to make an Eloquent model withable, add the trait to the model and define a list of relations that can be eagerly loaded using request paramaters.\nYou can either define a `$withable` property or implement a `getWithableRelations` method if you want to execute some logic to define\nlist loadable relations.\n\n```php\nuse Jedrzej\\Withable\\WithableTrait;\n\nclass Post extends Eloquent\n{\n\tuse WithableTrait;\n\t\n\t// either a property holding a list of loadable relations...\n\tprotected $withable = ['owner', 'forum'];\n\t\n\t// ...or a method that returns a list of loadable relations\n\tprotected function getWithableRelations()\n\t{\n\t\treturn ['owner', 'forum'];\n\t}\n}\n```\n\nIn order to make all relations loadable put an asterisk `*` in the list:\n\n```php\nprotected $withable = ['*'];\n```\n\n## Loading relations\n\n`WithableTrait` adds a `withRelations()` scope to the model - you can pass it a list of relations to load as you would pass to Eloquent's `with()` method:\n\n```php\n// return all posts with the user who created them and forum where they were posted\nPost::withRelations(['owner', 'forum'])-\u003eget();\n// return all posts with the user who created them\nPost::withRelations('owner')-\u003eget();\n```\n\n or it will use `Request::all()` as default. In the URL you can pass a list of relations to load or a single relation in the `with` parameter:\n\n```php    \n// return all posts with the user who created them and forum where they were posted by appending to the URL\n?with[]=owner\u0026with[]=forum\n// return all posts with the user who created them\n?with=owner\n//and then calling\nPost::withRelations()-\u003eget();\n```\n\n## Loading scoped relations\nIf you have defined local scopes in a related model, you can load this relation with the scope applied. E.g. in order to eagerly\nload posts with their approved comments:\n\n```php\n// define local scope in Comment model\npublic function scopeApproved($query) {\n  $query-\u003ewhereIsApproved(true);\n}\n\n// append the following to the URL in order to load \"comments\" relation with \"approved\" local scope applied\n?with=comments:approved\n\n// load the posts in your controller\nPost::withRelations()-\u003eget();\n```\n\n\n## Additional configuration\n\nIf you are using `with` request parameter for other purpose, you can change the name of the parameter that will be\n interpreted as a list of relations to load by setting a `$withParameterName` property in your model, e.g.:\n\n```php\nprotected $withParameterName = 'relations';\n```\n\nIf you need to execute additional logic to define a list of relations to load (e.g. permission checking),\nyou can implement `getWithRelationsList()` method in your model and make that return list of relations:\n\n```php\npublic function getWithRelationsList() {\n  return Request::get('relations');\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedrzej%2Fwithable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedrzej%2Fwithable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedrzej%2Fwithable/lists"}