hhxsv5 / laravel-s

LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.
MIT License
3.83k stars 471 forks source link

laravel 组合laravels后,DB查询返回数组或对象问题? #103

Closed dafa168 closed 5 years ago

dafa168 commented 5 years ago
  1. Tell us your software version How to know it?

    # PHP
    php -v
    # Swoole
    php --ri swoole
    # Laravel
    grep 'laravel/framework' composer.json
    # Lumen
    grep 'laravel/lumen-framework' composer.json
    Software Version
    PHP 7.2.8
    Swoole 4.2.0
    Laravel/Lumen 5.7
  2. Detail description about this issue(error/log)

我在这个文件中加入了此段代码: app/Providers/EventServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Database\Events\StatementPrepared;
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\ExampleEvent' => [
            'App\Listeners\ExampleListener',
        ],
    ];

    public function boot()
    {
        parent::boot();

        /**
!!!就是这里!!!
         * **设置POD返回数组**
         */
        Event::listen(StatementPrepared::class, function ($event) {
            $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
        });

    }
}

问题: 当我在 laravel本身的命令行中使用DB::get()->toArray() 查询出来得到的是二位数组里面是一维数组。

但是在 laravels 启动的异步任务中使用 DB::get()->toArray() 查询出来得到的是二位数组里面是对象 ?

感觉是laravels中应该是没有执行到上面的事件代码吧

  1. Give us a reproducible code block and steps

    //TODO: Your code
hhxsv5 commented 5 years ago

未复现,得到的二维数组每个元素是正常数组。 image image image image

hhxsv5 commented 5 years ago

https://laravel-china.org/docs/laravel/5.3/queries/1187#retrieving-results https://laravel-china.org/docs/laravel/5.2/queries/1137#retrieving-results

Laravel 5.3之前版本是返回数组,5.3及以后版本返回Collection集合对象。