Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitshrestha221's avatar

Export to XLXS

I am using maatwebsite Excel plugin

    public function exportCategory(){
        $categories=Categories::where('parent_id','0')
                                        ->where('type','category')
                                        ->where('status','enabled')
                                        ->select('id', 'category','parent_id','type','status')
                                        ->get();


        Excel::create('Export Data', function($excel) use($categories) {
            $excel->sheet('Sheet 1', function($sheet) use($categories) {
                $sheet->fromArray($categories);
            });
        })->export('xlsx');
    }

The result is blank excel sheet..

0 likes
9 replies
geowrgetudor's avatar

Do you get any results for $categories before passing it to the Excel::create method?

Also fromArray() might accept only instance of Model or array. Not sure, but it might not accept a Collection of models.

tomopongrac's avatar

Try convert $categories to array

$categories->toArray();
amitshrestha221's avatar

@tomi i did this.. still blank..


public function exportCategory(){
        
        $categories=Categories::where('status','enabled')
                                ->select('id', 'category','parent_id','type','status')
                                ->get()->toArray();

        return Excel::create('categories', function($excel) use ($categories) {

            $excel->sheet('mySheet', function($sheet) use ($categories){
                $sheet->fromArray($categories);
            });

        })->download('xls');
        // Excel::create('Categories', function($excel) use($categories) {
        //     $excel->sheet('categories', function($sheet) use($categories) {
        //         $sheet->fromArray($categories);
        //     });
        // })->export('xls');
    }

amitshrestha221's avatar

@tomi it is working in chrome but not in firefox..?? Now I am even more confused whats going on..

tomopongrac's avatar

Try with only one use ($categories)

return Excel::create('categories', function($excel) use ($categories) {

            $excel->sheet('mySheet', function($sheet){
                $sheet->fromArray($categories);
            });

        })->export('xls');
}

BRIENO's avatar

Try doing it this way.

public function exportCategory(){
        
        $categories=Categories::where('status','enabled')
                                ->select('id', 'category','parent_id','type','status')
                                ->get();

   $categoryArray = array(['id','category','parent_id','type','status']);

 
   foreach($categories as $category){
          $data = array($category->id, $category->category,
                                  $category->parent_id,$category->type,
                                   $category->status);
           array_push($categoryArray, $data);
  
             }


        Excel::create('categories', function($excel) use ($categoryArray) {

            $excel->sheet('mySheet', function($sheet) use ($categoryArray){
                $sheet->fromArray($categoryArray,null,'A1',false,false);
            });

        })->download('xls');
      
    }

}

1 like

Please or to participate in this conversation.