3

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.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Jsnow
  • 375
  • 1
  • 6
  • 14
  • Long story short it eventually returns [https://laravel.com/api/5.3/Illuminate/Database/Connection.html](https://laravel.com/api/5.3/Illuminate/Database/Connection.html) – Ohgodwhy Sep 04 '16 at 05:18
  • @Ohgodwhy i figure it out sir that the query syntax came from https://laravel.com/api/5.3/Illuminate/Database/Connection.html but i need and explanation how DatabaseManager object from app['db'] can use Connection class method. – Jsnow Sep 04 '16 at 06:13

1 Answers1

1

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);
}
Sherif
  • 1,477
  • 1
  • 14
  • 21
  • i'm getting clearer sir thanks to you but i have one more question, when i var_dump app['db']->connection() it returning mysqlconnection object. in databasemanager class there is connection method with $name paramater. how app['db']->connection() can return mysql type connection when i never set databasemanager connection method with mysql type before. thnk u sir – Jsnow Sep 04 '16 at 10:05
  • in `connection()` method if you didn't pass a name the method gets the default connection specified in the `.env` file or the `config/database.php` file. which in this case mysql type... dig deeper into the source code and you'll understand. – Sherif Sep 04 '16 at 12:20