How to add database to constructor using Laravel IoC

I want to use the DB class for a transaction with an IoC container.

use Illuminate\Database\Connection as DB; public function __construct(DB $db) { $this->db = $db; } 

But when the db class is used, I got an error.

 Unresolvable dependency resolving [Parameter #0 [ <required> $dsn ]] in class PDO 

I know I have to connect something, but I don’t know what?

+5
source share
1 answer

You need to change

 use Illuminate\Database\Connection as DB; 

in

 use Illuminate\Database\DatabaseManager as DB; 

I thought you could use the DB facade here, but actually you cannot. You need to explicitly pass here the class that is behind the facade, looking at http://laravel.com/docs/4.2/facades#facade-class-reference

+7
source

Source: https://habr.com/ru/post/1204628/


All Articles