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

File "UpdateMemberDates.php"

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

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Member;
use Illuminate\Support\Facades\Validator;

class UpdateMemberDates extends Command
{
    protected $signature = 'members:update-dates {file}';
    protected $description = 'Updates the start and end dates for members based on their email ID from a given CSV file';

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

    public function handle()
    {
        $filePath = $this->argument('file');

        if (!file_exists($filePath)) {
            $this->error("File does not exist.");
            return;
        }

        $handle = fopen($filePath, "r");
        if ($handle !== FALSE) {
            // Assuming CSV headers are email_id, start_date, end_date
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $email = $data[0];
                $startDate = $data[1];
                $endDate = $data[2] ?? null;  // Allow endDate to be null

                // Validate the data
                $validator = Validator::make([
                    'email' => $email,
                    'start_date' => $startDate,
                    'end_date' => $endDate
                ], [
                    'email' => 'required|email',
                    'start_date' => 'required|date',
                    'end_date' => 'nullable|date'  // Make end_date nullable
                ]);

                // if ($validator->fails()) {
                //     $this->error("Data validation failed for email: $email - " . $validator->errors());
                //     continue;
                // }

                // Update the members
                Member::where('email_id', $email)->update([
                    'start_date' => $startDate,
                    'end_date' => $endDate
                ]);
                
                $this->info("Updated dates for $email");
            }
            fclose($handle);
        }
    }
}