How to Export CSV File in Laravel

Websolutionstuff | Apr-30-2021 | Categories : Laravel

In this post we will see how to export CSV file in laravel, Export csv file in laravel is most common function and many time we are using this function using pluging or readymate function.

Here, i will give you example export csv file in laravel 8 without using any type plugin in laravel.So, let's see how to export csv file in laravel 8.

 

Step 1: Add Route

Add get method in your route file.

Route::get('/examples', 'ExampleController@exportCsv');

 

Step 2: In Blade File

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

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

.

Step 3 : Add Script in JS file

Now, add below script.

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

 

Step 3 : Add Function in Controller

Copy below code in your controller for export csv file in laravel 8.

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);
    }

 

Recommended Post
Featured Post
Carbon Add Months To Date In Laravel
Carbon Add Months To Date In L...

In this article, we will see an example of carbon add months to date in laravel. Here, we will give you a simple&nb...

Read More

Dec-05-2020

How to Integrate Razorpay Payment Gateway in Laravel
How to Integrate Razorpay Paym...

In this article, we will see the most important and exciting toping about how to integrate razorpay payment gateway in l...

Read More

Jan-06-2021

Laravel 9 Create Zip File And Download
Laravel 9 Create Zip File And...

In this article, we will see laravel 9 create a zip file and download it. Laravel provides ZipArchive class fo...

Read More

May-02-2022

Laravel 9 Form Validation Example
Laravel 9 Form Validation Exam...

 In this tutorial, we will see laravel 9 form validation example. For any incoming data, we need to validate i...

Read More

Feb-12-2022