Skip to content

Commit a16264d

Browse files
committed
add uuid
1 parent 4ca9ec3 commit a16264d

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Finller\Conversation\Conversation;
4+
use Illuminate\Support\Str;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
Schema::table('conversations', function (Blueprint $table) {
17+
$table->uuid()->nullable();
18+
});
19+
20+
Conversation::all()->each(function (Conversation $conversation) {
21+
$conversation->uuid = (string) Str::uuid();
22+
$conversation->saveQuietly([
23+
'touch' => false
24+
]);
25+
});
26+
27+
28+
Schema::table('conversations', function (Blueprint $table) {
29+
$table->uuid()->nullable(false)->change();
30+
});
31+
}
32+
33+
/**
34+
* Reverse the migrations.
35+
*/
36+
public function down(): void
37+
{
38+
Schema::table('conversations', function (Blueprint $table) {
39+
$table->dropColumn('uuid');
40+
});
41+
}
42+
};

src/Conversation.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Illuminate\Database\Eloquent\Relations\HasOne;
1313
use Illuminate\Database\Eloquent\Relations\MorphTo;
1414
use Illuminate\Foundation\Auth\User;
15+
use Illuminate\Support\Str;
1516

1617
/**
1718
* @property int $id
19+
* @property string $uuid
1820
* @property Collection $users
1921
* @property ?int $owner_id
2022
* @property ?User $owner
@@ -30,17 +32,20 @@ class Conversation extends Model
3032
{
3133
use HasFactory;
3234

33-
protected $fillable = [
34-
'owner_id',
35-
'metadata',
36-
];
35+
protected $guarded = [];
3736

3837
protected $casts = [
3938
'metadata' => AsArrayObject::class,
4039
];
4140

4241
protected static function booted(): void
4342
{
43+
static::creating(function (Conversation $conversation) {
44+
if (empty($conversation->uuid)) {
45+
$conversation->uuid = (string) Str::uuid();
46+
}
47+
});
48+
4449
/**
4550
* Cleanup pivot records
4651
* We choose to not use onCascade Delete at the database level for 3 reasons:

src/ConversationServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function configurePackage(Package $package): void
2020
'create_conversations_table',
2121
'create_messages_table',
2222
'create_conversation_user_table',
23+
'add_uuid_to_conversations_table',
2324
])
24-
->hasConfigFile()
25-
->runsMigrations();
25+
->hasConfigFile();
2626
}
2727
}

0 commit comments

Comments
 (0)