【问题标题】:How to get a result from a query without the tag "stdClass Object" in Laravel?如何从 Laravel 中没有标签“stdClass Object”的查询中获取结果?
【发布时间】:2013-06-23 00:15:40
【问题描述】:

我将 Laravel v4.0 与 MySQL 一起使用。我正在使用 Query Builder 进行查询以检索要由控制器管理的信息。问题是任何查询的结果都包含一个名为 stdClass Object 的标记:

Array ( [0] => stdClass Object ( [srv_codctrl] => KIRAVA ) [1] => stdClass Object ( [srv_codctrl] => KIRBAS ) [2] => stdClass Object ( [srv_codctrl] => KIRMAN ) )

我可以使用将数据转换为数组的格式来操作数据,但我认为应该有一种方法可以在没有该标记的情况下获得结果。

有人知道解决办法吗?

【问题讨论】:

    标签: php mysql sql casting laravel


    【解决方案1】:

    app/config/database.php中的PDO Fetch样式更改为PDO::FETCH_ASSOC

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */
    
    'fetch' => PDO::FETCH_ASSOC
    

    这里有更多关于PDO Fetch style的信息。

    【讨论】:

    • 非常感谢您的帮助!
    【解决方案2】:

    假设上面的数组在一个名为数组的变量中:

    foreach($array as $obj)
    {
        echo $obj->srv_codctrl;
    }
    

    echo $array[0]->srv_codctrl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多