php 类 stdClass 的对象无法转换为字符串 - laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30260076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:40:07  来源:igfitidea点击:

Object of class stdClass could not be converted to string - laravel

phpexcellaravel

提问by Alexander Nikolov

I am following this documentation

我正在关注此文档

to implement export to Excel in my laravel 4 project.

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

So am trying to generate excel file from array like this:

所以我试图从这样的数组生成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');

But this raises exception:

但这引发了异常:

  Object of class stdClass could not be converted to string

What am I doing wrong ?

我究竟做错了什么 ?

UPDATE:

更新:

Tried this:

试过这个:

$data = get_object_vars($data);

which results in:

这导致:

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

This:

这个:

$data = (array)$data;

Results in the initial error.

导致初始错误。

回答by kunal

Try this simple in one line of code:-

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

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

Hope it helps :)

希望能帮助到你 :)

回答by Damien Pirsy

$datais indeed an array, but it's made up of objects.

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

Convert its content to array before creating it:

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

$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(....

回答by Kishor Kurian

You might need to change your object to an array first. I dont know what exportdoes, but I assume its expecting an array.

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

You can either use

您可以使用

get_object_vars()

get_object_vars()

Or if its a simple object, you can just typecast it.

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

$arr = (array) $Object;

$arr = (array) $Object;

回答by Edujugon

If you have a Collection of stdClass objects, you could try with this:

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

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

回答by Japhet Johnson

This is easy all you need to do is something like this Grab your contents like this

这很简单,您只需要做这样的事情 像这样抓取您的内容

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

回答by Güney Saramal?

I was recieving the same error when I was tring to call an object element by using another objects return value like;

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

$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

The above code was throwing the error and I tried many ways to fix it like; calling this part $this->array1->countryin another function as return value, (string), taking it into quotations etc. I couldn't even find the solution on the web then i realised that the solution was very simple. All you have to do it wrap it with curly brackets and that allows you to target an object with another object's element value. like;

上面的代码抛出了错误,我尝试了很多方法来修复它;$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};

If anyone facing the same and couldn't find the answer, I hope this can help because i spend a night for this simple solution =)

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