JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "ImportCSV.php"

Full Path: /var/www/lionsclub/core/app/Console/Commands/ImportCSV.php
File size: 1.38 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use League\Csv\Reader; // Ensure you have league/csv installed via Composer

class ImportCSV extends Command
{
    protected $signature = 'import:csv {path}';
    protected $description = 'Imports a CSV file into the database';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $path = $this->argument('path');
        if (!file_exists($path) || !is_readable($path)) {
            $this->error('CSV file does not exist or is not readable');
            return;
        }

        $csv = Reader::createFromPath($path, 'r');
        $csv->setHeaderOffset(0); // Assumes the first row of the CSV is the header

        $records = $csv->getRecords(); // Get all records from the CSV
        foreach ($records as $record) {
            foreach ($record as $key => $value) {
                $record[$key] = trim($value);
            }
            DB::table('members')->insert([
                'district' => $record['district'],
                'full_name' => $record['name'],
                'email_id' => $record['email'],
                'mobile_numbers' => $record['mobile_numbers'],
                'position' => $record['position'],
            ]);
        }

        $this->info('CSV data has been imported successfully!');
    }
}