{"id":19845221,"url":"https://github.com/rapidwebltd/improved-polymorphic-eloquent-builder","last_synced_at":"2025-05-01T21:30:25.977Z","repository":{"id":62533296,"uuid":"79561349","full_name":"rapidwebltd/Improved-Polymorphic-Eloquent-Builder","owner":"rapidwebltd","description":"🔨 Improved Polymorphic Eloquent Builder","archived":false,"fork":false,"pushed_at":"2019-02-13T12:56:52.000Z","size":15,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-03T02:25:13.723Z","etag":null,"topics":["eloquent","laravel","laravel-5-package","polymorphic-relationships","relationships"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rapidwebltd.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":"2017-01-20T13:28:57.000Z","updated_at":"2019-12-24T17:05:43.000Z","dependencies_parsed_at":"2022-11-02T15:00:54.896Z","dependency_job_id":null,"html_url":"https://github.com/rapidwebltd/Improved-Polymorphic-Eloquent-Builder","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidwebltd%2FImproved-Polymorphic-Eloquent-Builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidwebltd%2FImproved-Polymorphic-Eloquent-Builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidwebltd%2FImproved-Polymorphic-Eloquent-Builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidwebltd%2FImproved-Polymorphic-Eloquent-Builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rapidwebltd","download_url":"https://codeload.github.com/rapidwebltd/Improved-Polymorphic-Eloquent-Builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224278483,"owners_count":17285079,"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":["eloquent","laravel","laravel-5-package","polymorphic-relationships","relationships"],"created_at":"2024-11-12T13:06:55.076Z","updated_at":"2024-11-12T13:06:56.636Z","avatar_url":"https://github.com/rapidwebltd.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Improved Polymorphic Eloquent Builder\n\n![Packagist](https://img.shields.io/packagist/dt/rapidwebltd/Improved-Polymorphic-Eloquent-Builder.svg)\n\nAttempting to use `whereHas` queries with standard Eloquent polymorphic relationships will fail, due to Eloquent being unable\nto determine the correct model to retrieve. You may receive an error similar to the one below as Eloquent tries to build the \nquery using columns from the model without including its table name.\n\n```\nQueryException in Connection.php line 662:\nSQLSTATE[42S22]: Column not found: 1054 Unknown column 'column_name' in 'where clause'\n```\n\nThe 'Improved Polymorphic Eloquent Builder' is a class which extends the Eloquent Builder class that is built in to Laravel 5.1. \nIt enables limited use of the `whereHas` method to query Eloquent polymorphic relationships.\n\n## Requirements\n\nYou must be using Laravel 5.1 as your framework and Eloquent as your ORM.\n\n## Installation\n\nSimply require this package, using Composer, in the root directory of your project.\n\n```\ncomposer require rapidwebltd/improved-polymorphic-eloquent-builder\n```\n\nThen change any Eloquent models using polymorphic relationships to extend the `\\RapidWeb\\ImprovedPolymorphicEloquentBuilder\\Model`\nclass. This will usually be any model(s) containing methods which return `morphTo()` relationship(s). An example class is shown below.\n\n```php\nclass Variation extends \\RapidWeb\\ImprovedPolymorphicEloquentBuilder\\Model\n{\n  public function model()\n  {\n    return $this-\u003emorphTo();\n  }\n}\n```\n\n## Usage\n\nWhen performing a `whereHas` query, you must specify the morph types as the 5th argument. Morph types refers to an array\nof the polymorphic types you wish to filter by. These should be presented as strings equal to one or more of the\npossible values the `[...]_type` field of the polymorphic relationship in question.\n\nThe example below selects all `variation` records that have related `model` record  of type `bags`, with a `brand_id` of 2.\n\n```php\nVariation::whereHas('model', function($query) use ($brandId) {\n  $query-\u003ewhere('brand_id', 2);\n}, '\u003e=', 1, ['bags'])-\u003eget();\n```\n\nA database structure for this example would be similar to the following. The polymorphic fields are highlighted in **bold**.\n\n| variations                                  | bags        | sunglasses     |\n|---------------------------------------------|-------------|----------------|\n| id                                          | **id**      | **id**             |\n| **model_type** ('bags', 'sunglasses', etc.) | brand_id    | brand_id       |\n| **model_id**                                | material_id | lens_colour_id |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frapidwebltd%2Fimproved-polymorphic-eloquent-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frapidwebltd%2Fimproved-polymorphic-eloquent-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frapidwebltd%2Fimproved-polymorphic-eloquent-builder/lists"}