Skip to content
Advertisement

Binding Interfaces to Implementations in Eloquent relationships

According to “binding-interfaces-to-implementations” it is possible to bind an interface with its implementation, as follows:

JavaScript

So when I call the next line, I will get an User object: ????????

JavaScript

However… when I try to do the same process with an Eloquent relationship:

JavaScript

it, obviously, gives me the following error: ????

JavaScript

The same error if I try to instance an interface as follow:

JavaScript

I think there should be a way to delegate to an abstraction (interface) instead of using a concrete class. In this way it would be much easier to modularize the application and make it more decoupled.

Has anyone done something similar in Laravel?

Advertisement

Answer

I believe you can achieve what you want with a static function call, placed somewhere in your app.

JavaScript

This will solve your problem, as you can now bind the class and resolve it at run time.

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