Search code examples
phplaravel-5modeldatabase-connectionlaravel-5.8

Laravel debug the Model connection to check if the env function is parsing the credentials properly


Working on multi-database, multi-connection project in Laravel 5.8.x version.

I created a Model for fetching data from specific database using specific connection.

I have defined the connection variable in Model file as:

protected $connection = '[ConnName]';

And I have the said connection set in config/database.php as:

'connections' => [
    '[ConnName]' => [
        'username' => env('username'),
        ....
    ],
    ....
]

Still when I try to load the Model's data into Controller's Index action as:

$modelData = Model::orderBy('created_at', 'DESC');
exit(print_r(compact($modelData)));
return view('entity.index', compact('modelData'));

It prints null/no records, even though there is data in the database linked with model thro' said connection.

Can anyone suggest anyway to cross-check, if the connection credentials are properly parsed thro' env method or just how to print the utilized connection in controller ?


Solution

  • Make sure that the Model has ->get()

    $modelData = Model::orderBy('created_at', 'DESC')->get();
    
    dd($modelData);