0

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

Nemanja
  • 119
  • 1
  • 3
  • 16

2 Answers2

1

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);
Bak87
  • 219
  • 3
  • 9
  • You sir, are my HERO! Thanks a lot <3 – Nemanja Jun 25 '18 at 13:53
  • `$assets = Asset::where('id', $company_id)->get()->toArray(); ` This line still doesn't work. I only get first asset from table. – Nemanja Jun 25 '18 at 13:57
  • How many records do you have in that table that has id = $company_id? If you have only one, you are going to get one. – Bak87 Jun 25 '18 at 14:13
0

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

    $assets = Asset::where('id', $company_id)->get();
ali
  • 832
  • 6
  • 11