Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get laravel DB connection to php connection?

I need to receive laravel DB connection to variable to make concrete pure SQL php demands on a database. Desired code:

<?php 
// this is not working
$conn = DB::connection();

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Desired result: "Connected successully"

I need it because I have very complex SQL demands that I need to put in one query, not using laravel query syntax (I believe it is better approach, but I have complex queries and need to use pure query text to execute, not laravel "->" syntax)

I need to get laravel DB connection to not always often establish second php connection like with PDO or writing down DB credentials. If PDO connection to DBS from laravel exists, it could be useful to obtain to php for me too.

DB::connection()->name

returns a name of DB, but thats no connection :/

I was looking for it but nowhere found solution for that, could someone help me find correct answer please? (maybe it is not important, but I use mysql)

like image 390
Marek Bernád Avatar asked Mar 26 '17 00:03

Marek Bernád


People also ask

How do I configure Laravel's database services?

The configuration for Laravel's database services is located in your application's config/database.php configuration file. In this file, you may define all of your database connections, as well as specify which connection should be used by default.

What is the difference between multiple database connections in Laravel?

They are basically has same connection but only different in database name. Laravel has ability to run in multiple database connection even in across different database engine. We can create migration based on connection we made before.

Can I use a SQLite database with Laravel sail?

By default, Laravel's sample environment configuration is ready to use with Laravel Sail, which is a Docker configuration for developing Laravel applications on your local machine. However, you are free to modify your database configuration as needed for your local database. SQLite databases are contained within a single file on your filesystem.

What do database URLs look like in Laravel?

An example database URL may look something like the following: These URLs typically follow a standard schema convention: For convenience, Laravel supports these URLs as an alternative to configuring your database with multiple configuration options.


2 Answers

You can use raw query in laravel for executing queries as you wish. If you need connection instance anyway you can create a class and implement ConnectionResolverInterface:

use Illuminate\Database\ConnectionResolverInterface as Resolver
class connection implements Resolver {

}

then you can get connection:

$connector = new connection();
$connection = $connector->connection();

If you're using your model connection you can get connection this way:

$user = new User();
$user->getConnection();

Also you can make a new connection by using ConnectionFactory:

$connection = new Illuminate\Database\Connector\ConnectionFactory();

for more information you can see laravel API doc

like image 54
MoPo Avatar answered Sep 30 '22 00:09

MoPo


Try this:

$pdo = $eloquentConnection->getConnection()->getRawPdo();

Cheers

like image 37
Matt Avatar answered Sep 30 '22 02:09

Matt