Skip to content
Advertisement

Using functions in Laravel macros

I would like to extend Eloquent Builder to have a support for search function. Inside my service provider I’ve added the following:

JavaScript

which works. I can now call a search method on my model.

The issue I’m having is that the logic within it is getting a rather complex and I wouldn’t like it to be a single large script, however splitting to functions doesn’t work since scope when inside a macro callback is actually from Eloquent Builder.

So this doesn’t work:

JavaScript

Is there a way to use functions without going over the hassle of extending complete Eloquent Builder?

Advertisement

Answer

I have ended up creating a class which would contain the complete logic I need. I see this as a fairly good OOP practice.

JavaScript

For anyone interested in details, you can check out my JSON search package.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement