Skip to content

Commit bf89d16

Browse files
committed
fix: migrations
1 parent f6855c2 commit bf89d16

6 files changed

+114
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Homestead.json
1515
Homestead.yaml
1616
npm-debug.log
1717
yarn-error.log
18+
todo.md
1819
/auth.json
1920
/.fleet
2021
/.idea

database/factories/GroupFactory.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Group>
9+
*/
10+
class GroupFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => $this->faker->name,
21+
'description' => $this->faker->sentence,
22+
];
23+
}
24+
}

database/factories/MessageFactory.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Group;
6+
use App\Models\User;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
9+
/**
10+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Message>
11+
*/
12+
class MessageFactory extends Factory
13+
{
14+
/**
15+
* Define the model's default state.
16+
*
17+
* @return array<string, mixed>
18+
*/
19+
public function definition(): array
20+
{
21+
// Message User to User
22+
$senderId = $this->faker->randomElement([0, 1]);
23+
if ($senderId === 0) {
24+
$senderId = $this->faker->randomElement(User::where('id', '!=', 1)->pluck('id')->toArray());
25+
$receiverId = 1;
26+
27+
} else {
28+
$receiverId = $this->faker->randomElement(User::where('id', '!=', 1)->pluck('id')->toArray());
29+
}
30+
31+
// Message User to Group
32+
$groupId = null;
33+
34+
if ($this->faker->boolean(30)) {
35+
$groupId = $this->faker->randomElement(Group::pluck('id')->toArray());
36+
$group = Group::find($groupId);
37+
38+
$senderId = $this->faker->randomElement($group->users->pluck('id')->toArray());
39+
$receiverId = null;
40+
}
41+
42+
return [
43+
'sender_id' => $senderId,
44+
'receiver_id' => $receiverId,
45+
'group_id' => $groupId,
46+
'content' => $this->faker->realText(500),
47+
'created_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
48+
];
49+
50+
}
51+
}

database/migrations/0001_01_01_000000_create_users_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up(): void
1818
$table->timestamp('email_verified_at')->nullable();
1919
$table->string('password');
2020
$table->boolean('is_admin')->default(false);
21-
$table->boolean('blocked_at')->nullable();
21+
$table->timestamp('blocked_at')->nullable();
2222
$table->rememberToken();
2323
$table->timestamps();
2424
});

database/migrations/2025_02_28_035337_create_conversations_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public function up(): void
1212
{
1313
Schema::create('conversations', function (Blueprint $table) {
1414
$table->id();
15-
;
1615
$table->string('name');
1716
$table->foreignId('user_id1')->constrained('users');
1817
$table->foreignId('user_id2')->constrained('users');

database/seeders/DatabaseSeeder.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Database\Seeders;
44

5+
use App\Models\Message;
56
use App\Models\User;
67
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
8+
use App\Models\Group;
79
use Illuminate\Database\Seeder;
10+
use Illuminate\Support\Facades\Hash;
811

912
class DatabaseSeeder extends Seeder
1013
{
@@ -13,11 +16,42 @@ class DatabaseSeeder extends Seeder
1316
*/
1417
public function run(): void
1518
{
16-
// User::factory(10)->create();
1719

1820
User::factory()->create([
19-
'name' => 'Test User',
20-
'email' => '[email protected]',
21+
'name' => 'Okono Wilfried',
22+
'email' => '[email protected]',
23+
'password' => Hash::make('Test1234'),
24+
'is_admin' => true,
25+
'email_verified_at' => now()
2126
]);
27+
28+
User::factory()->create([
29+
'name' => 'Piper Mba',
30+
'email' => '[email protected]',
31+
'password' => Hash::make('password'),
32+
'is_admin' => false,
33+
'email_verified_at' => now()
34+
]);
35+
36+
User::factory()->create([
37+
'name' => 'Jowil',
38+
'email' => '[email protected]',
39+
'password' => Hash::make('password'),
40+
'is_admin' => false,
41+
'email_verified_at' => now()
42+
]);
43+
44+
User::factory(28)->create();
45+
46+
for ($i = 0; $i < 5; $i++) {
47+
$group = Group::factory()->create(['owner_id' => 1]);
48+
49+
$users = User::inRandomOrder()->limit(rand(3, 7))->pluck('id');
50+
$group->users()->attach(array_unique([1, ...$users]));
51+
}
52+
53+
Message::factory(1000)->creae();
54+
55+
$messzzzzzz
2256
}
2357
}

0 commit comments

Comments
 (0)