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

后端 未结 6 1164
野性不改
野性不改 2021-02-18 17:04

I am following this documentation

to implement export to Excel in my laravel 4 project.

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



        
相关标签:
6条回答
  • 2021-02-18 17:13

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

    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(....
    
    0 讨论(0)
  • 2021-02-18 17:19

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

    $data = $data->map(function ($item){
                return get_object_vars($item);
            });
    
    0 讨论(0)
  • 2021-02-18 17:20

    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';
    
    0 讨论(0)
  • 2021-02-18 17:31

    Try this simple in one line of code:-

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

    Hope it helps :)

    0 讨论(0)
  • 2021-02-18 17:34

    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->country in 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 = 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 =)

    0 讨论(0)
  • 2021-02-18 17:35

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

    You can either use

    get_object_vars()

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

    $arr = (array) $Object;

    0 讨论(0)
提交回复
热议问题