Laravel Update database with CSVfile | Mass update DB based on an array from an CSV file | Updating the table from a csv file | Laravel 9 Upload CSV File

To update a database table in Laravel with data from a CSV file, you can follow these steps:

  1. Create a Migration for the Database Table (If Not Already Done):

    Ensure you have a database table that you want to update with data from the CSV file. If the table doesn't exist, you can create it using Laravel's migration feature. Run the following command to generate a migration:

    bash
php artisan make:migration create_your_table_name

Edit the generated migration file to define the table's structure, and then run the migration to create the table:

bash
  • php artisan migrate
  • Create a Route and Controller:

    Create a route in the routes/web.php file that points to a controller method responsible for handling the CSV upload and update process:

    php
  • Route::get('/upload-csv', 'CsvUpdateController@index'); Route::post('/upload-csv', 'CsvUpdateController@update');

    Next, generate a controller called CsvUpdateController:

    bash
  • php artisan make:controller CsvUpdateController
  • Create a Form for Uploading CSV:

    In your Blade view file, create a form that allows users to upload a CSV file. Typically, this form would be associated with the update route you defined earlier:

    html
  • <form action="/upload-csv" method="POST" enctype="multipart/form-data"> @csrf <input type="file" name="csv_file" accept=".csv"> <button type="submit">Upload CSV</button> </form>
  • Implement the CSV Update Logic:

    In the CsvUpdateController, implement the logic to read the uploaded CSV file, update the database table with the data from the CSV file, and handle any necessary error checks. Here's a simplified example:

    php
    1. // CsvUpdateController.php use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CsvUpdateController extends Controller { public function index() { return view('upload-csv'); } public function update(Request $request) { $request->validate([ 'csv_file' => 'required|file|mimes:csv,txt', ]); $file = $request->file('csv_file'); $csvData = array_map('str_getcsv', file($file->getPathname())); foreach ($csvData as $row) { $recordToUpdate = ['column_name' => $row[0]]; // Assuming the CSV contains data for updating a single column DB::table('your_table_name')->where('unique_column', $row[1])->update($recordToUpdate); } return redirect('/upload-csv')->with('success', 'Database updated successfully'); } }

      In this example, we assume that the CSV file contains data for updating a single column, and we use the update method to update records in the database based on a unique column value.

      Make sure to replace 'your_table_name', 'column_name', and 'unique_column' with the actual table name, column name, and the unique column you want to use for matching records.

    2. Handle Validation and Display Errors:

      You can add validation and error handling to ensure the uploaded file is a valid CSV file and handle any potential errors during the update process.

    3. Display Success Message:

      After a successful update, you can redirect the user back to the upload form and display a success message to confirm that the database was updated successfully.

    With these steps, you can create a feature in your Laravel application to update a database table with data from a CSV file.

    Comments