查看官方文档:https://laravel.com/docs/5.4/upgrade
Fetch Mode
Laravel no longer includes the ability to customize the PDO "fetch mode" from your configuration files. Instead, PDO::FETCH_OBJ is always used. If you would still like to customize the fetch mode for your application you may listen for the new Illuminate\Database\Events\StatementPrepared event:
Event::listen(StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(...);
});
从5.4开始, 不能再通过配置文件的方式去改变fetch mode了。而使用了事件监听设置去实现该配置。
打开app/Providers/EventServiceProvier.php, 按照官方文档,先引入Illuminate\Database\Events\StatementPrepared类,然后在boot方法中加入上方样例代码,最终代码如下:
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Events\StatementPrepared; //引入类库 用于查询数据对象转数组 20191014
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
/**
* 这里添加对象转数组的设置 20191014
*/
Event::listen(StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);
});
}
}