【问题标题】:Object of class stdClass could not be converted to string - laravel类 stdClass 的对象无法转换为字符串 - laravel
【发布时间】:2015-05-15 12:54:07
【问题描述】:

我关注this documentation

在我的 laravel 4 项目中实现导出到 Excel。

所以我试图从这样的数组中生成 excel 文件:

//$results is taken with db query

$data = array();
foreach ($results as $result) {
   $result->filed1 = 'some modification';
   $result->filed2 = 'some modification2';
   $data[] = $result;
}
Excel::create('Filename', function($excel) use($data) {

$excel->sheet('Sheetname', function($sheet) use($data) {

    $sheet->fromArray($data);

      });

})->export('xls');

但这会引发异常:

  Object of class stdClass could not be converted to string

我做错了什么?

更新:

试过这个:

$data = get_object_vars($data);

导致:

get_object_vars() expects parameter 1 to be object, array given

这个:

$data = (array)$data;

导致初始错误。

【问题讨论】:

  • 我认为您的配置遗漏了一些东西。仔细检查您的 app/config.php 文件中的服务提供商和别名。

标签: php excel laravel


【解决方案1】:

用一行代码试试这个简单的方法:-

$data= json_decode( json_encode($data), true);

希望对你有帮助:)

【讨论】:

  • 这应该是正确的答案,由于它的简单性,这一定是正确的答案。为我工作。非常感谢。
  • 我认为这会更好,因为它也可以将任何嵌套对象转换为数组。
【解决方案2】:

当我试图通过使用另一个对象返回值来调用对象元素时,我收到了同样的错误;

$this->array1 = a json table which returns country codes of the ip
$this->array2 = a json table which returns languages of the country codes

$this->array2->$this->array1->country;// Error line

上面的代码抛出了错误,我尝试了很多方法来修复它;在另一个函数中调用这部分$this->array1->country 作为返回值(string),将其放入引号等。我什至在网上找不到解决方案,然后我意识到解决方案非常简单。你所要做的就是用大括号将它包裹起来,这样你就可以用另一个对象的元素值来定位一个对象。喜欢;

$this->array1 = a json table which returns country codes of the ip
$this->array2 = a json table which returns languages of the country codes

$this->array2->{$this->array1->country};

如果有人遇到同样的问题并且找不到答案,我希望这会有所帮助,因为我花了一个晚上来寻找这个简单的解决方案 =)

【讨论】:

    【解决方案3】:

    您可能需要先将对象更改为数组。我不知道export 做了什么,但我认为它期待一个数组。

    你可以使用

    get_object_vars()

    或者,如果它是一个简单的对象,您可以直接对其进行类型转换。

    $arr = (array) $Object;

    【讨论】:

    • 你能 var_dump($data) 和它的对象还是数组?而且您还没有首先提到哪一行产生错误Object of class stdClass could not be converted to string
    【解决方案4】:

    $data确实是一个数组,但它是由对象组成的。

    在创建之前将其内容转换为数组:

    $data = array();
    foreach ($results as $result) {
       $result->filed1 = 'some modification';
       $result->filed2 = 'some modification2';
       $data[] = (array)$result;  
       #or first convert it and then change its properties using 
       #an array syntax, it's up to you
    }
    Excel::create(....
    

    【讨论】:

      【解决方案5】:

      如果你有一个 stdClass 对象的集合,你可以试试这个:

      $data = $data->map(function ($item){
                  return get_object_vars($item);
              });
      

      【讨论】:

        【解决方案6】:

        这很简单,你需要做的就是像这样抓住你的内容

          $result->get(filed1) = 'some modification';
          $result->get(filed2) = 'some modification2';
        

        【讨论】:

          猜你喜欢
          • 2020-03-16
          • 1970-01-01
          • 2021-05-28
          • 1970-01-01
          • 2017-09-21
          • 1970-01-01
          • 1970-01-01
          • 2011-04-06
          相关资源
          最近更新 更多