Set background color to rows in Php laravel excel

I’m using laravel framework to export an excel. I’m using a manual procedure to set background colors to particular rows. Here’s the sample excel data

Sample Excel Data

In the above example, I want to set a color to the entire rows that contains a text in second column. Likewise adding a BG color to the entire rows that contains a text in second column.

Excel after adding background colors

use Illuminate\Support\Facades\App;
use Maatwebsite\Excel\Facades\Excel;

 public function handle() {
    $excel_name = 'download excel';
    return Excel::create($excel_name, function ($excel) {
        $excel->sheet('Sheet 1', function ($sheet) {
            $this->add_heading($sheet);
            $regions = $this->getData();
            foreach ($regions as $region) {
                $this->add_data_to_excel($sheet, $region);
            }
            $this->add_color_to_sheet($sheet);
        });
      });
   }

public function add_color_to_sheet($sheet) {
$sheet->cells('A1:E1', function ($cells) {
     $cells->setBackground('#3490ED');
 });
$sheet->cells('A2:E2', function ($cells) {
    $cells->setBackground('#6D8947');
  });
$sheet->cells('A3:E3', function ($cells) {
     $cells->setBackground('#0DF3FE');
  });
$sheet->cells('A8:E8', function ($cells) {
     $cells->setBackground('#0DF3FE');
  });
$sheet->cells('A11:E11', function ($cells) {
    $cells->setBackground('#6D8947');
 });
 $sheet->cells('A12:E12', function ($cells) {
      $cells->setBackground('#0DF3FE');
 });
 $sheet->cells('A14:E14', function ($cells) {
     $cells->setBackground('#0DF3FE');
 });
 };

How can I do it dynamically by using conditions to set the background colors?

Sponsor our Newsletter | Privacy Policy | Terms of Service