27

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:

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

Alexander Nikolov
  • 1,871
  • 7
  • 27
  • 49
  • I think you missed something with your configuration. Double check your `app/config.php` file for Service Provider and aliases. – Shravan Sharma May 15 '15 at 13:14

7 Answers7

60

Try this simple in one line of code:-

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

Hope it helps :)

kunal
  • 4,122
  • 12
  • 40
  • 75
30

$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(....
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
10

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;

Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43
Kishor Kurian
  • 185
  • 1
  • 7
  • Can you var_dump($data) and its an object or an array? And you havent mentioned which line produces the error `Object of class stdClass could not be converted to string` in the first place. – Kishor Kurian May 15 '15 at 13:09
2

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

$data = $data->map(function ($item){
            return get_object_vars($item);
        });
Edujugon
  • 91
  • 2
  • 6
1

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

Güney Saramalı
  • 791
  • 1
  • 10
  • 19
0

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

toArrayDeep worked for me

$data = $query->get()->toArrayDeep();