Search code examples
laravellaravel-facade

How laravel facade can use DB and DB instance at the same time?


I use DB facade. I see it can use in DB::connection from Illuminate\Database\DatabaseManager and DB::select from Illuminate\Database\Connection.

enter image description here

I don't understand how one facade can apply two serivce?

Thank you for your asking!


Solution

  • One Facade can point to multiple classes for different methods. This is possible through the magic method __call.

    Here's the __call method of the DatabaseManager class:

     /**
     * Dynamically pass methods to the default connection.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->connection()->$method(...$parameters);
    }
    

    The DB facade points to the DatabaseManager class and if the method is not found the DatabaseManager then forwards the call to the Connection class.