Reputation: 121
I wonder where all of that SQL queries are located which provide database connection, data manupilation and others. I tried to dive deeper and deeper into Laravel source code but all I found is just Laravel methods. I know this is very useless but is also very interesting to know.
Upvotes: 0
Views: 900
Reputation: 101
The SQL connection is in .env file in the root directory of your project.If you want to override the connection from .env then you can set in config > database.php
inside the project root directory.
Upvotes: 0
Reputation: 1712
You shouldn't have to mess with the SQL directly. That is a feature of Laravel. If you are curious thoug, the Database connection is in (if you haven't published your vender files) /vendor/laravel/framework/src/Illuminate/DataBaseManger.php
in make connection starting at line 101 where we have a call:
return $this->factory->make($config, $name);
Which calls:
return $this->createSingleConnection($config);
Which does:
$pdo = $this->createPdoResolver($config);
return $this->createConnection(
$config['driver'], $pdo, $config['database'], $config['prefix'], $config
);
So we have a $pdo object being passed to createConnection which does:
return new MySqlConnector;
Which does the connection:
public function connect(array $config)
{
$dsn = $this->getDsn($config);
$options = $this->getOptions($config);
// We need to grab the PDO options that should be used while making the brand
// new connection instance. The PDO options control various aspects of the
// connection's behavior, and some might be specified by the developers.
$connection = $this->createConnection($dsn, $config, $options);
if (! empty($config['database'])) {
$connection->exec("use `{$config['database']}`;");
}
$this->configureEncoding($connection, $config);
// Next, we will check to see if a timezone has been specified in this config
// and if it has we will issue a statement to modify the timezone with the
// database. Setting this DB timezone is an optional configuration item.
$this->configureTimezone($connection, $config);
$this->setModes($connection, $config);
return $connection;
}
But why is that so convoluted and complicated? Because, you can do complex things like having a "connection" that uses one database for read and another for write, or switching to a different type of database (maybe a non-relational database) and not have to worry about changing all your code.
There are two places. Illuminate has the Database Query Builder and Eloquent has the ORM (which uses the QueryBuilder under the hood I believe. But the execution happens in Illuminate/Query/Builder
get() calls the protected method runSelect() which calls the $this->connection->select(...)
which executes the built PDO statement on line 330 of Illuminate/Database/Connection
.
Upvotes: 1
Reputation: 436
If you want to see how the queries are built, just look for the file Builder.php
located on vendor\laravel\framework\src\Illuminate\Database\Query
folder.
Then you can check all other files in the Query
folder to see what's happening on your eloquent queries.
You will never have to touch those files, just use the documentation to know how to use the Query Builder.
Upvotes: 0