Framework: Eloquent Builder doesn't allow Mixins

Created on 18 Apr 2019  路  6Comments  路  Source: laravel/framework

  • 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);

Most helpful comment

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

All 6 comments

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!

@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.

@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);

@Ciaro,

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

progmars picture progmars  路  3Comments

ghost picture ghost  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

jackmu95 picture jackmu95  路  3Comments

shopblocks picture shopblocks  路  3Comments