67 lines
2.3 KiB
PHP
67 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\License;
|
|
use App\Models\Role;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class PrivatBobanSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
$privat = Tenant::firstOrCreate(
|
|
['name' => 'Privat Boban'],
|
|
['type' => 'private', 'active' => true]
|
|
);
|
|
|
|
$license = License::withoutGlobalScopes()->firstOrCreate(
|
|
['tenant_id' => $privat->id, 'type' => 'per_student'],
|
|
['seats' => 5, 'expires_at' => now()->addYear(), 'active' => true]
|
|
);
|
|
|
|
$users = [
|
|
['email' => 'boban@lernschiff.com', 'name' => 'Boban Blaskovic', 'role' => 'parent'],
|
|
['email' => 'kind.boban@lernschiff.com', 'name' => 'Kind Boban', 'role' => 'child'],
|
|
];
|
|
|
|
$createdUsers = [];
|
|
foreach ($users as $data) {
|
|
$user = \App\Models\User::withoutGlobalScopes()->firstOrCreate(
|
|
['email' => $data['email']],
|
|
['name' => $data['name'], 'tenant_id' => $privat->id, 'password' => Hash::make('password')]
|
|
);
|
|
|
|
$role = Role::where('name', $data['role'])->first();
|
|
DB::table('role_user')->updateOrInsert(
|
|
['user_id' => $user->id],
|
|
['role_id' => $role->id, 'created_at' => now(), 'updated_at' => now()]
|
|
);
|
|
|
|
if ($data['role'] === 'child') {
|
|
DB::table('license_assignments')->insertOrIgnore([
|
|
'license_id' => $license->id,
|
|
'user_id' => $user->id,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
$createdUsers[$data['role']] = $user;
|
|
}
|
|
|
|
// Link parent ↔ child (cross-tenant-capable via withoutGlobalScope on children() relation)
|
|
if (isset($createdUsers['parent'], $createdUsers['child'])) {
|
|
$parent = $createdUsers['parent'];
|
|
$child = $createdUsers['child'];
|
|
// Avoid duplicate attach
|
|
if (!$parent->children()->withoutGlobalScope(\App\Scopes\TenantScope::class)->where('child_id', $child->id)->exists()) {
|
|
$parent->children()->attach($child->id);
|
|
}
|
|
}
|
|
}
|
|
}
|