关于DB查询结果的疑惑?

1. 运行环境

1). 当前使用的 Laravel 版本?

Laravel 9.18

2). 当前使用的 php/php-fpm 版本?

PHP 版本:

8.15

2. 问题描述?

我这有很多需要动态用表名查询的表,所以不用模型,提议让我用模型的就算了。

$userList = DB::connection('test')->table('user')->get();
dump($userList);
//如,打印出一个Collection ,其中items为一个stdClass的数组。
$user = DB::connection('test')->table('user')->first();
dump($user);
//如,打印出一个stdClass,其对象只能通过
echo $user->name;
echo $user->sex;

//如果想删除其中某些属性,只能通过
unset($user->passwd);
unset($user->hash);
unset($user->xx);
//我现在使用方法
$user = collect($user);
$user->only(['a','b','c']);

问题:有什么地方可以配置DB查询的返回的类型吗,first返回array或者collect都行?
get(),它返回的这个items里的stdClass转为array或者collect都行。
这个stdClass,真心太难用了,啥啥都没得。。。,toArray()也不行;
要是每次查询完都来一个collect($user),或者(array)$user,也太蛋疼了

db
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

添加事件

<?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;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    Event::listen(StatementPrepared::class, function ($event) {
      $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
    });
    }

    /**
     * Determine if events and listeners should be automatically discovered.
     *
     * @return bool
     */
    public function shouldDiscoverEvents()
    {
        return false;
    }
}
2年前 评论
____Laravel (楼主) 2年前
讨论数量: 2

添加事件

<?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;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    Event::listen(StatementPrepared::class, function ($event) {
      $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
    });
    }

    /**
     * Determine if events and listeners should be automatically discovered.
     *
     * @return bool
     */
    public function shouldDiscoverEvents()
    {
        return false;
    }
}
2年前 评论
____Laravel (楼主) 2年前

添加事件

<?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;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    Event::listen(StatementPrepared::class, function ($event) {
      $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
    });
    }

    /**
     * Determine if events and listeners should be automatically discovered.
     *
     * @return bool
     */
    public function shouldDiscoverEvents()
    {
        return false;
    }
}
2年前 评论
____Laravel (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!