Laravel 5.4 - How to set PDO Fetch Mode?

佐手、 提交于 2019-11-30 14:02:56

Go to: app/Providers/EventServiceProvider.php

Add this to the top of the file:

use Illuminate\Database\Events\StatementPrepared;

In the boot method add:

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
});
$dbh=DB::getPdo();
$sth = $dbh->prepare("SHOW STATUS
            WHERE Variable_name = 'Max_used_connections'
            OR Variable_name = 'Max_used_connections_time'
            OR Variable_name = 'Threads_connected' ");
$sth->execute(); 
$result = $sth->fetch(PDO::FETCH_CLASS);
print_r($result);

Try this. worked for me. You require only DB trait(use DB;).

Add default fetch mode in config/database.php

return [
    'fetch' => PDO::FETCH_CLASS,
    ...
];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!