Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eloquent Builder doesn't allow Mixins #28262

Closed
mikemand opened this issue Apr 18, 2019 · 6 comments
Closed

Eloquent Builder doesn't allow Mixins #28262

mikemand opened this issue Apr 18, 2019 · 6 comments

Comments

@mikemand
Copy link
Contributor

  • Laravel Version: 5.8.12
  • PHP Version: 7.2.15
  • Database Driver & Version: MySQL (what do you mean by version? MySQL 5.7.25? mysqlnd 5.0.12-dev?)

Description:

Eloquent Builder allows macros but doesn't allow mixins:

In ForwardsCalls.php line 50:

  Call to undefined method Illuminate\Database\Eloquent\Builder::mixin()

Steps To Reproduce:

Try adding a mixin within a service provider: \Illuminate\Database\Eloquent\Builder::mixin(new \App\Mixins\SearchRelationship);

@driesvints
Copy link
Member

Heya, thanks for submitting this.

This seems like a feature request or an improvement. It's best to post these at the laravel/ideas repository to get support for your idea. After that you may send a PR to the framework. Please only use this issue tracker to report bugs and issues with the framework.

Thanks!

@mikemand
Copy link
Contributor Author

@driesvints,

I was under the impression that the Macroable trait provided the mixin functionality too. Would this not be a bug? Eloquent's Builder gets its macro ability from the base Query Builder (via ForwardsCalls), so the mixin ability should also be present.

@Ciaro
Copy link

Ciaro commented Jun 12, 2019

@mikemand

You could always do something like this:

Builder::macro('mixin', function ($mixin) {
   $methods = (new ReflectionClass($mixin))->getMethods(
      ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
   );

   foreach ($methods as $method) {
      $method->setAccessible(true);

      Builder::macro($method->name, $method->invoke($mixin));
    }
 });

Builder::mixin(New YourMixins);

@mikemand
Copy link
Contributor Author

@Ciaro,

Thank you! This will work until this bug gets fixed.

@A-H-Pooladvand
Copy link

Any update on this? or any reason that why eloquent\builder doesn't support mixins?

@carcinocron
Copy link

I have the opposite problem, mixins applied to eloquent models are being forwarded to Illuminate\Database\Query\Builder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants