Nemanja
Nemanja

Reputation: 129

Exporting Excel getting this error Trying to get property 'id' of non-object

I'm using Maatwebsite Laravel Excel package 3.0. This is my InvoicesExport.php

    class InvoicesExport implements FromCollection
{
    public function collection()
    {
        $company_id = Auth::user()->company_id;
         $assets = Asset::where('id', $company_id)->get()->toArray();    
         $assets_array = array();
        if(!$assets->isEmpty()){
         foreach($assets as $asset){
             $assets_array[] = array(
             'Asset ID' => $asset->id,
             'Asset Name' => $asset->name,
             'Description' => $asset->description,
             'Serial Number' => $asset->serialno,
             'External ID' => $asset->external_id,
             'Location' => $asset->location,
             'Expiry Date' => $asset->expiry_date,
             'Owner' => $asset->owner,
             'Status' => $asset->status,
             'Updated at' => $asset->updated_at
             );
            }
        }
        //dd($assets_array); 
        return $assets_array;
    }
}

And i keep getting this error

Trying to get property 'id' of non-object

And this is my function in controller

public function excel() 
    {    
          return Excel::download(new InvoicesExport, 'invoices.xlsx');
    }

My code looks like this now and I keep getting this error.

Call to a member function each() on array

When i use dd($assets_array) I get those 2 items that i have in database, so i think maybe it's problem in my RETURN

Upvotes: 0

Views: 1671

Answers (2)

Bak87
Bak87

Reputation: 219

It seems, the problem is outside of the InvoicesExport Class.

Why don't you change the type of return?

instead of returning

return $assets_array;

Do it like this:

return collect($assets_array);

Upvotes: 1

ali
ali

Reputation: 862

you convert the collection to array and try to access it as object just remove the toArray

    $assets = Asset::where('id', $company_id)->get();

Upvotes: 0

Related Questions