Search code examples
phplaraveleloquentlaravel-5.3

Using Eloquent Model class. Where is the implementation of static method "where"?


Consider this code

$match_get1 = Model::where(SyncVariableConstant::MAIN_ID_FIELD_NAME, $sqlId)->get();

I really don't understand where is the definition of the where static method in the Model:: class context. Model class doesn't not implement the method where (source).

Obviously, I think this is a kind of php mecanism i am not familar with...

Does sommeone have an explaination?

Thanks in advance


Solution

  • Models generally extend Eloquent facade, which is just a shortcut to underlying app('db') instance.

    The where() method you are looking for resides in \Illuminate\Database\Query\Builder namespace. This namespace is used by Eloquent ORM, the code of which lies within \Illuminate\Database\Eloquent\Builder namespace. Both classes have where() methods. If you check the latter class, you will see that in it's constructor it injects the former class as a dependency (i.e. Dependency Injection).

    In a nutshell, \Illuminate\Database\Eloquent\Builder is high-level user of \Illuminate\Database\Query\Builder, both of which have where() implemented.

    And how high-level \Illuminate\Database\Eloquent\Builder is invoked? As another user here already stated, through magic-getter __call()