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

pickab00's avatar

Maatwebsite/Laravel-Excel How to manipulate a row if the result is equal to a specific value

I am using Maatwebsite/Laravel-Excel Package to export some data in my database. Here is where i have gone so far.

What i am doing here is, i am taking the "user_id", "roomnumber" & "verified" column from my "cleans table. I have joined the "users" table so that i get my user name instead of the "user_id". Its a simple query.

Whats confusing me though is, how can i manipulate the "verified" row (its of course an array) if the result is equal to "1". I want to be able to change the background color of the row if the value is 1.

Here is a line from the docs which is related to manipulating a specific row.

$sheet->setStyle(array(
   'font' => array(
   'name'      =>  'Calibri',
   'size'      =>  12,
   'bold'      =>  true
   )
));

I am not sure how to continue from there. How should i state the if else statement.

Here is the code i have so far:

$clean = Clean::select('users.name', 'cleans.roomnumber', 'cleans.verified')->join('users','users.id','=','cleans.user_id')->get();

Excel::create('Report', function($excel) use($clean) {
    $excel->sheet('Sheet 1', function($sheet) use($clean) {

        $sheet->fromArray(array(
            array('Room Attendant Name', 'Room Number', 'Verified')
        ));


        $sheet->fromArray($clean, null, 'A1', false, false);
    });
})->export('xls');

Hope this information was enough.

Thanks in advance.

0 likes
8 replies
Corban's avatar
Corban
Best Answer
Level 6

It's very likely that there would be a better and cleaner way, but I'm assuming you are using a foreach to navigate through all the rows, so why not just add a condition?

Excel::create('Report', function($excel) use($clean){
  $excel->sheet('Sheet 1', function($sheet) use($clean){
    $sheet->row(1, array('Room Attendant Name', 'Room Number', 'Verified'));
   
    $i = 2;

    foreach ($clean as $example) {

      if($example->col3 == 1){
        $sheet->cell('C'.$i, function($color){
         $color->setBackground('#008000');
       });
      }

      $sheet->row($i, array($example->relation->name .' '.$example->relation->lastname, $example->col2, $example->col3));
      $i++;
    }

    $sheet->setAutoFilter();
  });
})->download('xlsx');

I think something like that should work, but can't assure it at 100% since I don't usually manipulate the style of cells. If you find a better way please let me know since I think in the future I'd probably need it as well.

1 like
pickab00's avatar

@Corban I cant seem to figure out why but i am getting the invalid response error:

The webpage at http://test.com/admin/export might be temporarily down or it may have moved permanently to a new web address. ERR_INVALID_RESPONSE

Here is the code updated:

$clean = Clean::with('users')->get();

    Excel::create('Report', function($excel) use($clean){
        $excel->sheet('Sheet 1', function($sheet) use($clean){
            $sheet->row(1, array('Room Attendant Name', 'Room Number', 'Verified'));

            $i = 2;

            foreach ($clean as $cleans) {

                if($cleans->verified == 1){
                    $sheet->row($i, function($color) {
                        $color->setBackground('#008000');
                    });
  }

                $sheet->row($i, array($cleans->users->name, $cleans->roomnumber, $cleans->verified));
                $i++;
            }

            $sheet->setAutoFilter();
        });
    })->download('xlsx');

There isn't any stacktrace either on laravels log

1 like
Corban's avatar

@pickab00 Maybe is taking too long to generate? Check set_time_limit() to see if there's the problem.

Edit: also while doing some research I found that you could be missing a , try downloading it as xls instead of xlsx

1 like
pickab00's avatar

@Corban I don't think it is taking time. I am away from my pc at the moment. This error comes instantly. There is no delay or processing going on whatsoever

1 like
Corban's avatar

@pickab00 mmmm, ok. Try the xlsone when you can, I'll research a bit more when I have time.

The function seems to be right, I have tried it and works perfectly (the green is actually a bit dark btw, and makes the row hard to read, my fault).

pickab00's avatar

@Corban haha. We'll get back to the color issue way later xD Anyways thanks. I will leave a post as soon as i get some time to test it.

pickab00's avatar

@Corban I solved the issue. the problem was with my relation. Not the code. Your proposed solution works for me. But i must confess, i need to update my question. I accidentally wrote, "row" instead of column. i wanted the color to change only on that specific column.

eg: if the id 1's verified is equal to 1 then change only that specific columns color. Like id 1's only verified column to be green. Hope this makes sense. There is no mention of how to manipulate specific column on the docs.

Thanks

Corban's avatar

@pickab00 no problem, I have updated my initial answer changing the row with the cell. I used "C" since for what you have showed that's the column you want to edit, if not just change it.

Hope it helps.

Please or to participate in this conversation.