Laravel-excel: Get an error: Object of class stdClass could not be converted to string when export to excel using maatwebsite

Created on 5 Apr 2017  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

I want to show three tables, three tables are later to be exported, but I am having a problem here, please help me resolve my problem. Thank you.

public function downloadExcel($type)
{
$data = DB::table('tb_siswa')
->join('tb_ayah', 'tb_siswa.id', '=', 'tb_ayah.id_siswa')
->join('tb_ibu', 'tb_siswa.id', '=', 'tb_ibu.id_siswa')
->select('tb_siswa.nm_lengkap', 'tb_ayah.nm_ayah', 'tb_ibu.nm_ibu')
->get();
return Excel::create('Data siswa', function($excel) use ($data) {
$excel->sheet('datasiswa', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download($type);
}

Most helpful comment

I have faced the same kind of situation and here I came up with the idea.

Add this line before return function

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

Hope it helps!

All 7 comments

Try setting DB::setFetchMode(PDO::FETCH_ASSOC);

Hey @patrickbrouwers,

Since the DB::setFetchMode(PDO::FETCH_ASSOC); has been deprecated in Laravel 5.4 is it possible for you to reexamine this issue?
I think it would be possible to use get_object_vars() to access stdClass data that is returned by Laravels DB query builder. Currently the workaround for this issue is to use:

$data = DB::table('my_table')->get()->map(function ($item) {
    return get_object_vars($item);
});

I have faced the same kind of situation and here I came up with the idea.

Add this line before return function

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

Hope it helps!

Also there is no way to chunk query builder methods that returns an array such as DB::select for large queries.

I suggest a method that apply step wise addition of LIMIT to the SQL statement. i.e something like pagination.

An option for the workaround suggested by @patrickbrouwers is to add these lines to your AppServiceProvider in version >= 5.4.*

use Illuminate\Database\Events\StatementPrepared;
use Illuminate\Support\Facades\Event;
use PDO;

---
public function boot()
{
    Event::listen(StatementPrepared::class, function ($event) {
        $event->statement->setFetchMode(PDO::FETCH_ASSOC);
    });
}

Hey there, it seems that we're now registering an event on the StatementPrepared event when the SerializedQuery job runs: https://github.com/Maatwebsite/Laravel-Excel/blob/78f5bf3713db73f6ab1550cc63399fc6b24b29d9/src/Jobs/SerializedQuery.php#L56 ... when run in a queue worker this affects all statements thereafter.... that might be a bit too much of a good thing I suppose? I'm getting associative arrays from my QueryBuilder's ->get() now.

I have faced the same kind of situation and here I came up with the idea.

Add this line before return function

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

Hope it helps!

This helped me, but on really long data it seams to Exhaust memory.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daraghoshea picture daraghoshea  路  3Comments

lucatamtam picture lucatamtam  路  3Comments

octoxan picture octoxan  路  3Comments

contifico picture contifico  路  3Comments

muhghazaliakbar picture muhghazaliakbar  路  3Comments