nowox
nowox

Reputation: 29096

Cannot use 'with' in a Eloquent Query Builder Macro

I was attempting to use with on a Eloquent macro, but I get this error message:

Call to undefined method Illuminate\Database\Query\Builder::with()

The following works:

$builder = Model::query();
$builder->with('relationship')->toSql();

But if I do this inside a macro:

use Illuminate\Database\Query\Builder;

Builder::macro('foobar', function() {
    $this->with('relationship')->toSql());
});

$builder = Model::query();
$builder->foobar();

Then I get an error message.

Is this a bug or something that I do wrong?

Upvotes: 0

Views: 472

Answers (1)

nakov
nakov

Reputation: 14268

You are using the wrong import, try adding the macro to the eloquent builder instead:

use Illuminate\Database\Eloquent\Builder;

Upvotes: 2

Related Questions