laravel
  1. laravel-import-and-export-csv-file-in-laravel58

Import and Export CSV file in Laravel 5.8 - Laravel Misc.

In this article, we'll explore how to import and export CSV files in Laravel 5.8. CSV (Comma Separated Values) files are a popular file format for data exchange as they can be easily opened and edited in spreadsheet programs. Laravel provides built-in support for importing and exporting CSV files.

Importing CSV Files

Syntax

use Illuminate\Http\Request;

public function import(Request $request)
{
    $file = $request->file('file');
    $data = array_map('str_getcsv', file($file));

    foreach($data as $record) {
        // Process each record
    }
}

Example

public function import(Request $request)
{
    $file = $request->file('file');
    $data = array_map('str_getcsv', file($file));

    foreach($data as $row) {
        $user = new User;
        $user->name = $row[0];
        $user->email = $row[1];
        $user->save();
    }

    return redirect()->back();
}

Output

After the import process is complete, the user data will be stored in the database.

Explanation

The import function reads the uploaded file and converts it into an array of data, which can be easily processed. Each row of data is then inserted into the database using the Eloquent ORM interface.

Use

Importing CSV files can be used in a variety of applications, such as importing user data, importing product data, and importing financial data.

Important Points

  • The imported file must be in CSV format.
  • The first row of the CSV file must contain the column headers.
  • Each subsequent row represents a record.

Exporting CSV Files

Syntax

use App\User;
use Illuminate\Support\Facades\Response;

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

    $users = User::all();
    $columns = array('Name', 'Email');

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

        foreach($users as $user) {
            $row = array(
                $user->name,
                $user->email
            );
            fputcsv($file, $row);
        }

        fclose($file);
    };

    return Response::stream($callback, 200, $headers);
}

Example

use App\User;
use Illuminate\Support\Facades\Response;

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

    $users = User::all();
    $columns = array('Name', 'Email');

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

        foreach($users as $user) {
            $row = array(
                $user->name,
                $user->email
            );
            fputcsv($file, $row);
        }

        fclose($file);
    };

    return Response::stream($callback, 200, $headers);
}

Output

After the export function is called, the user data will be downloaded as a CSV file.

Explanation

The export function fetches the user data from the database and converts it into a CSV format. The CSV file is then downloaded as a response to the user's request.

Use

Exporting CSV files can be used in a variety of applications, such as exporting user data, exporting product data, and exporting financial data.

Important Points

  • The exported data will be downloaded as a CSV file.
  • The first row of the CSV file will contain the column headers.
Published on: