feat: Implement user-branch relationship and update user management views
- Added a many-to-many relationship between users and branches in User model. - Updated user creation and editing views to support multiple branch selection. - Modified user index view to display associated branches. - Created UserBranch model to manage user-branch associations. - Added migration for user_branches table with foreign key constraints. - Implemented seeder to populate user_branches based on existing user branch data. Run this command: - php artisan migrate - php artisan module:seed Usermanagement --class=UserBranchesSeeder
This commit is contained in:
38
database/seeders/UserBranchesSeeder.php
Normal file
38
database/seeders/UserBranchesSeeder.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Usermanagement\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Usermanagement\Models\User;
|
||||
|
||||
class UserBranchesSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$users = User::all();
|
||||
|
||||
foreach ($users as $user) {
|
||||
if ($user->branch_id) {
|
||||
$exists = DB::table('user_branches')
|
||||
->where('user_id', $user->id)
|
||||
->where('branch_id', $user->branch_id)
|
||||
->exists();
|
||||
|
||||
if (!$exists) {
|
||||
DB::table('user_branches')->insert([
|
||||
'user_id' => $user->id,
|
||||
'branch_id' => $user->branch_id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->command->info('User branches seeded successfully.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user