How we upload the csv file via custom module

Stefan Bogdanescu
Founder & Senior Architect · 19 August 2026

Creating a Custom CSV Upload Module in Laravel

In this blog post, we’ll guide you through creating a custom module in your Laravel application to upload a CSV file via a dedicated menu option in the admin backend. We’ll cover setting up the custom menu, displaying the form with a file input field for CSV files, processing the submitted file in the controller, and reading its contents.

Setting Up the Custom Menu

To begin, you need to create a new menu item that will trigger the CSV upload functionality. In your app/Http/Controllers/Admin/MenuController.php file, add the following method:

public function getMenuItems()
{
    return [
        'admin.csv-upload' => [
            'title' => trans('csv-upload::menu.title'),
            'priority' => 50,
            'url' => route('admin.csv-upload.index'),
            'permissions' => ['csv-upload'],
            'icon' => 'fa-file-csv',
        ],
    ];
}

This code defines a new menu item with the title “CSV Upload”, priority 50, and the URL pointing to the CSV upload index page. Make sure you have the necessary permission set up.

Displaying the File Input Form

Next, create a new blade view file at resources/views/admin/csv-upload/index.blade.php to display the form with the file input field:

<div class="content">
  <div class="clearfix"></div>

  @if(count($errors) > 0)
    <div class="alert alert-danger">
      <button type="button" class="close" data-dismiss="alert">×</button>
      {{ trans('admin::messages.validation') }}<br/>
      <ul>
        @foreach ($errors->all() as $error)
          <li>{{ $error }}</li>
        @endforeach
      </ul>
    </div>
  @endif

  <form action="{{ route('admin.csv-upload.store') }}" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <div class="col-md-6">
      <div class="form-group{{ $errors->has('csv_file') ? ' has-error' : '' }}">
        <label for="csv_file">{{ trans('csv-upload::form.csv-file') }}</label>
        <input type="file" id="csv_file" name="csv_file" accept=".csv" class="form-control">
      </div>
    </div>
    <div class="col-md-6 text-right">
      <button type="submit" class="btn btn-primary">{{ trans('admin::form.submit') }}</button>
    </div>
  </form>
</div>

This blade view displays a file input field with the accept attribute set to .csv and includes a submit button.

Processing the Submitted File in the Controller

In your app/Http/Controllers/Admin/CsvUploadController.php, implement the store method to handle the submitted CSV file:

public function store(Request $request)
{
    $request->validate([
        'csv_file' => 'required|file|mimes:csv,txt,csv'
    ]);

    $file = $request->file('csv_file');
    $path = $file->store('uploads', 'public');

    // Read the CSV file content
    $handle = fopen(storage_path("app/public/{$path}"), "r");
    if ($handle) {
        while (($buffer = fgets($handle, 4096)) !== false) {
            // Process each line of the CSV file as needed
        }
        fclose($handle);
    }

    session()->flash('success', trans('csv-upload::messages.file-uploaded'));
    return redirect()->route('admin.csv-upload.index');
}

This method validates the uploaded file, stores it using Laravel’s built-in file storage functionality, and then reads its content line by line. You can process each line according to your specific requirements.

Reading the CSV File Content

In the while loop of the store method, you can use a library like PHPExcel or Laravel’s built-in CSV handling functions to parse the file content. For example:

use League\Csv\Reader;

$reader = Reader::createFromPath(storage_path("app/public/{$path}"));
foreach ($reader->getRecords() as $record) {
    // Process each record (row of data)
}

Conclusion

In this blog post, we’ve demonstrated how to create a custom CSV upload module in your Laravel application. We covered setting up the custom menu, displaying the file input form, processing the submitted file in the controller, and reading its content.

Remember that proper validation is essential when handling user-uploaded files. Always sanitize and validate data before processing it further.

For more advanced examples and use cases related to CSV processing in Laravel, consider checking out the official documentation: Laravel Documentation on File Storage

Happy coding!

Our network