58 lines
1.2 KiB
PHP
58 lines
1.2 KiB
PHP
|
|
<?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;
|
||
|
|
}
|
||
|
|
}
|