Files
Global-Jain/app/Console/Commands/memberIdGenerate.php

58 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2025-11-05 10:37:10 +05:30
<?php
namespace App\Console\Commands;
use App\Models\SanghMember;
use Illuminate\Console\Command;
class memberIdGenerate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'add:member-id';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add member id to sangh member table';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$membersGroupedBySangh = SanghMember::orderBy('id')->get()->groupBy('sangh_id');
foreach ($membersGroupedBySangh as $sanghId => $members) {
$counter = 1;
foreach ($members as $member) {
$newMemberId = str_pad($counter, 4, '0', STR_PAD_LEFT);
$member->update(['member_id' => $newMemberId]);
$counter++;
}
}
$this->info('Member IDs added to columns successfully.');
return 0;
}
}