Home » Php » php – How to inject Database into constructor with Laravel IoC

php – How to inject Database into constructor with Laravel IoC

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to use DB class for transaction with IoC Container.

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

But when use db class,i got an error.

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

I know, must bind something but i don’t know what?

How to&Answers:

You need to change

use Illuminate\Database\Connection as DB;

into

use Illuminate\Database\DatabaseManager as DB;

I thought you can use DB facade here but in fact you can’t. You need to explicitly pass here class that is behind the facade looking at http://laravel.com/docs/4.2/facades#facade-class-reference