Jsnow
Jsnow

Reputation: 375

Laravel Service provider and Service Container

In Laravel to access query, we use DB facades

DB::select()

from alanstorm website http://alanstorm.com/binding_objects_as_laravel_services I learned that DB facade use callstatic method that lead to DB::app['db']->select(). app is the Laravel service container object which all the services are bound into it. I use var_dump PHP method var_dump(app['db']) and I can see that the service container returns an Illuminate\Database\DatabaseManager object. from DatabaseManager class that implement ConnectionResolverInterface. I don't see the select method defined there. I want to ask how app['db'] can get access to select method.

Upvotes: 3

Views: 3006

Answers (1)

Sherif
Sherif

Reputation: 1537

DatabaseManager class implements __call() method if you call a method on that class that doesn't exist it's immediately passed as an argument to __call(), which is one of php's magic methods.

that calls connection class with the method you passed.

here's the method implementation in Illuminate\Database\DatabaseManager

/**
 * 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);
}

Upvotes: 1

Related Questions