Skip to content

Commit f750abd

Browse files
committed
More work on Support Ticket Single View
1 parent cd5feca commit f750abd

6 files changed

Lines changed: 119 additions & 6 deletions

File tree

app/Models/SupportTicket.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace App\Models;
44

5+
use App\Models\SupportTicket\Reply;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
810
use Illuminate\Database\Eloquent\SoftDeletes;
911

1012
class SupportTicket extends Model
@@ -23,7 +25,8 @@ protected static function booted()
2325
{
2426
static::creating(function ($ticket) {
2527
if (is_null($ticket->mask)) {
26-
28+
// @TODO Generate a unique mask for the ticket
29+
$ticket->mask = uniqid('ticket_');
2730
}
2831
});
2932
}
@@ -33,6 +36,12 @@ public function getRouteKeyName(): string
3336
return 'mask';
3437
}
3538

39+
public function replies(): HasMany
40+
{
41+
return $this->hasMany(Reply::class)
42+
->orderBy('created_at', 'desc');
43+
}
44+
3645
public function user(): BelongsTo
3746
{
3847
return $this->belongsTo(User::class);

app/Models/SupportTicket/Reply.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Models\SupportTicket;
4+
5+
use App\Models\SupportTicket;
6+
use App\Models\User;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10+
use Illuminate\Database\Eloquent\SoftDeletes;
11+
12+
class Reply extends Model
13+
{
14+
use HasFactory, SoftDeletes;
15+
16+
protected $fillable = [
17+
'support_ticket_id',
18+
'message',
19+
'attachments',
20+
'note',
21+
];
22+
23+
protected $casts = [
24+
'attachments' => 'array',
25+
'note' => 'boolean',
26+
];
27+
28+
public function user(): BelongsTo
29+
{
30+
return $this->belongsTo(User::class);
31+
}
32+
33+
public function supportTicket(): BelongsTo
34+
{
35+
return $this->belongsTo(SupportTicket::class);
36+
}
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Database\Factories\SupportTicket;
4+
5+
use App\Models\SupportTicket;
6+
use App\Models\SupportTicket\Reply;
7+
use App\Models\User;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
9+
use Illuminate\Support\Carbon;
10+
11+
class ReplyFactory extends Factory
12+
{
13+
protected $model = Reply::class;
14+
15+
public function definition(): array
16+
{
17+
return [
18+
'message' => $this->faker->paragraphs(2, true),
19+
'attachments' => null,
20+
'note' => false,
21+
'created_at' => Carbon::now(),
22+
'updated_at' => Carbon::now(),
23+
24+
'support_ticket_id' => SupportTicket::factory(),
25+
'user_id' => User::factory(),
26+
];
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use App\Models\SupportTicket;
4+
use App\Models\User;
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+
public function up(): void
11+
{
12+
Schema::create('replies', function (Blueprint $table) {
13+
$table->id();
14+
$table->foreignIdFor(SupportTicket::class);
15+
$table->foreignIdFor(User::class );
16+
$table->text('message');
17+
$table->json('attachments')->nullable();
18+
$table->boolean('note');
19+
$table->timestamps();
20+
$table->softDeletes();
21+
});
22+
}
23+
24+
public function down(): void
25+
{
26+
Schema::dropIfExists('replies');
27+
}
28+
};

database/seeders/SupportTicketSeeder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function run(): void
1515
{
1616
SupportTicket::factory()
1717
->count(10)
18+
->has(
19+
SupportTicket\Reply::factory()
20+
->state(['user_id' => 1])
21+
->count(5)
22+
)
1823
->create([
1924
'user_id' => 1,
2025
'status' => 'open',

resources/views/support/tickets/show.blade.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@
2626
<div class="mt-6 rounded-lg bg-white shadow dark:bg-gray-800">
2727
<div class="p-6">
2828
<h2 class="mb-4 text-xl font-medium">Messages</h2>
29-
@foreach([] as $message)
30-
<div class="mb-4 p-4 border rounded-lg {{ $message->is_from_user ? 'bg-blue-100' : 'bg-green-100' }}">
31-
<p><strong>{{ $message->user->name }}:</strong></p>
32-
<p>{{ $message->content }}</p>
33-
<p class="text-sm text-gray-500">{{ $message->created_at->format('d M Y, H:i') }}</p>
29+
@foreach($supportTicket->replies as $reply)
30+
<div class="flex flex-col w-full mb-6">
31+
<div class="relative w-full">
32+
<div class="{{ $reply->is_from_user ? 'bg-blue-100/50 dark:bg-blue-900/20' : 'bg-gray-100/70 dark:bg-gray-700/20' }} p-4 rounded-lg border {{ $reply->is_from_user ? 'border-blue-200/50 dark:border-blue-800/30' : 'border-gray-200/50 dark:border-gray-700/30' }}">
33+
<p class="font-medium text-gray-900 dark:text-gray-100">{{ $reply->user->name }}</p>
34+
<p class="mt-1 text-gray-800 dark:text-gray-200">{{ $reply->message }}</p>
35+
</div>
36+
</div>
37+
<div class="mt-1 {{ $reply->is_from_user ? 'text-right' : 'text-left' }}">
38+
<span class="text-xs text-gray-500 dark:text-gray-400">{{ $reply->created_at->format('d M Y, H:i') }}</span>
39+
</div>
3440
</div>
3541
@endforeach
3642
</div>

0 commit comments

Comments
 (0)