Building header row using foreach loop in Maatwebsite Excel with Laravel

I am trying to create an Excel file from HTML using Maatwebsite in Laravel, based on the selected MONTH AND YEAR.

Below is a snippet of my HTML view. The highlighted area represents the header, which generates data based on the chosen month and year. It displays all working days for that specific month and year by iterating through each date using a foreach loop.

https://i.stack.imgur.com/nlvcd.png

My challenge now is how to replicate this header structure in Maatwebsite similar to my HTML view above. I am unsure of how to iterate through dates within the header using a foreach loop.

I attempted the following code, but it only shows the last value obtained from my foreach loop:

foreach ($workdays as $workday) {
   $sheet->row(1, [$workday]);
}

https://i.stack.imgur.com/BsO99.png

Answer №1

 function captureAndSaveUserData($format)
{
    $data = User::get()->toArray();
    return Excel::create('userdataexcel', function($excel) use ($data) {
        $excel->sheet('userSheet', function($sheet) use ($data)
        {
            $sheet->cell('A1', function($cell) {$cell->setValue('3/12/2018');   });
            $sheet->cell('B1', function($cell) {$cell->setValue('4/12/2018');   });
            $sheet->cell('C1', function($cell) {$cell->setValue('5/12/2018');   });
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $i= $key+2;
                    $sheet->cell('A'.$i, $value['First_column']); 
                    $sheet->cell('B'.$i, $value['Second_column']); 
                    $sheet->cell('C'.$i, $value['Third_column']); 
                }
            }
        });
    })->download($format);
}

Source

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming iframe programming into jquery scripting

Currently, I have implemented an Iframe loading the contents within every 5 seconds. It works well, however, there is a consistent blinking effect each time it loads which can be quite bothersome. I am looking to replace the iframe with a scrolling div so ...

delete specific elements from an associative array

I am working with two arrays: $pool = array( 'foo' => array('foobar1'), 'bar' => array('foobar2'), 'lou' => array('foobar3'), 'zuu' => array('foobar ...

An application that combines the functionality of a desktop program with online server features and database integration

I'm currently developing a desktop application using electron and contemplating utilizing online storage to save data. I've scoured the internet for answers but can't seem to find reliable information. One possible approach is to integrate ...

"Receiving an 'undefined index' error when attempting to post in Ajax with

Need help with sending data from client to server using AJAX in PHP. I am facing an issue when trying the following code: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascrip ...

Combining two extremely large numbers encoded as base64 strings

Lately, I've been utilizing base64 strings in php to represent my enormous numbers. I'm curious if there is a library or built-in system that can assist me in performing arithmetic operations like addition, subtraction, multiplication, and divisi ...

Retrieving a specific column from a CSV file using PHP

In my PHP code, I have data stored in a csv format. The data looks something like this (please note that it is text with new line characters): $data = 'A,B,C,D,E,F,G,H 1,1,2014-12-10,5,1,2,0,2 2,7,2014-12-09,9,0,,7,2'; I need to figure ...

Combining PHP Variable with URL String

<td><input type="submit" onClick="window.location.href='https://www.'.$myValue.'.test.com'" value="Click!"></td> I am trying to create a button that will redirect to one of eight possible URLs based on a variable. How ...

What is the best way to send and configure GET information when the characters in a URI surpass 5,000?

Currently, I am utilizing a Laravel blade template. However, I encountered an error in my code when the size of the textarea item is quite large. I'm in search of a solution to resolve this issue. Hey everyone! Can you guide me on how to successfull ...

Page can be accessed using file_get_contents function but not with cURL

In the process of creating an API endpoint in PHP, a scenario arose where JSON data output from a specific page example.com/stats was causing issues with cURL requests compared to using file_get_contents(). How can I ensure that JSON data served in PHP is ...

What is the JQuery code to select a checkbox and display an input field?

I have created a function that retrieves a JSON response, and the data from this response is displayed in the input fields. This is a table. When I click the edit button, it sends a request to the master_get_items file and receives the response. Table sn ...

There seems to be a problem communicating securely with the server due to an SSL handshake failure with error code 14077410

$ch = curl_init(); $clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token"); curl_set ...

Automatically load and instantiate all classes in PHP

My current setup involves using php spl_autoload_extensions to automatically load all classes. The question I have is whether it's possible to instantiate classes simultaneously, eliminating the need for manual instantiation afterwards. Below is my b ...

There seems to be an issue with the functionality of the foreach loop

I am facing some issues with this script. I have checked everything and it seems correct, but for some reason the strpos syntax is not functioning as expected when dealing with the $_POST['category'] array which contains multiple selections. Coul ...

Are there any PHP *webpages* that have published their source code for access?

Finding PHP code examples is not a difficult task. However, most of the code available seems to be based on frameworks, libraries, etc. While these resources are valuable, I am interested in seeing an actual example of a complete website. My concern arise ...

Why does PHP_SELF fail when there is a $_get parameter in the URL?

Here's a scenario: My website's URL looks something like http://localhost/coursera/instructor/course.php?id=1&name=web%20development&section=overview On this particular page, there exists a form: <form action="<?php echo $_SERVER[ ...

Email forms without server interaction

Hey there, I'm looking to add a simple "+" or "-" voting form into an email newsletter. // plus button "+" <form action="/email-opinions/" method="POST"> <input type="hidden" value="GSC" name="strainName"> <input type="hidden" value="y ...

How to send a contact form in Wordpress with multiple attachments via email without using any plugins

The main objective is to enable users to submit their own content for new articles (including text and files) to the administrator's email. A form has been created for this purpose: <form class="form form-send" enctype="multipart/fo ...

When attempting to upload multiple files in CodeIgniter, the function is_uploaded_file() is expecting a string as parameter 1, but an array

I am having trouble uploading multiple files. Below is my form code: <form method="POST" action="<?=base_url()?>register/saverecord" enctype="multipart/form-data"> <input type="file" name="file_upload[]" multiple="multiple" value= ...

Troubleshooting: Issues with MagicSuggest's dependent multiple dropdown functionality

Currently implementing MagicSuggest for a custom dropdown feature with two dropdowns: category and subcategory. The goal is to have the subcategories populate based on the selected category. However, after selecting a category and then a subcategory, if ...

Using a Select Query to Retrieve Multiple Rows from a Join Table in a Collection in Magento

Is it possible to execute a Select Statement in _preparecollection within Magento when the main table is joined with another table that has 2 rows containing 1 parent ID? The current tables available are: Main Table: https://i.stack.imgur.com/DWrep.png ...