How to Export CSV file in Laravel Example

  82 views   1 week ago Laravel

In Laravel projects export csv and excel file from database more required. If you optate to exporting your data utilizing csv file in your projects version 5, 6 or 7 then here we make a simple step by step facile steps for export csv file utilizing php function not utilizing any library. In recent project we require to export the rows from the database in laravel but I checked in google more then sites utilizing library but if we engender csv utilizing php function then why we utilize any package. So guys lets start to export CSV Data in laravel example utilizing facile steps.

In first step you require to integrate a route in your web.php file.

Step 1: Add Route

Route::get('/tasks', 'TaskController@exportCsv');

Step 2: In Blade File

Add export button link in your blade file from where you want to export your tasks.

<span data-href="/tasks" id="export" class="btn btn-success btn-sm" onclick="exportTasks(event.target);">Export</span>

Step 3: In Js file

In this step add a export function in you js file to exporting data.

<script>
   function exportTasks(_this) {
      let _url = $(_this).data('href');
      window.location.href = _url;
   }
</script>

Step 4: In your Controller

In last step you need to add a function to export rows in csv in laravel 5, 6 or 7 application. Here we get all projects but you can modify your code which you want to export you can add in column and tasks.

public function exportCsv(Request $request)
{
   $fileName = 'tasks.csv';
   $tasks = Task::all();

        $headers = array(
            "Content-type"        => "text/csv",
            "Content-Disposition" => "attachment; filename=$fileName",
            "Pragma"              => "no-cache",
            "Cache-Control"       => "must-revalidate, post-check=0, pre-check=0",
            "Expires"             => "0"
        );

        $columns = array('Title', 'Assign', 'Description', 'Start Date', 'Due Date');

        $callback = function() use($tasks, $columns) {
            $file = fopen('php://output', 'w');
            fputcsv($file, $columns);

            foreach ($tasks as $task) {
                $row['Title']  = $task->title;
                $row['Assign']    = $task->assign->name;
                $row['Description']    = $task->description;
                $row['Start Date']  = $task->start_at;
                $row['Due Date']  = $task->end_at;

                fputcsv($file, array($row['Title'], $row['Assign'], $row['Description'], $row['Start Date'], $row['Due Date']));
            }

            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles