chat api correction

This commit is contained in:
pavithrak27 2026-06-02 11:04:43 +05:30
parent 1912b8d971
commit 9af2d5bc4e
8 changed files with 94 additions and 4 deletions

View File

@ -166,6 +166,8 @@ public function getMessages(Request $request, $id)
'time' => $msg->created_at->format('g:i A') . ($msg->created_at->isToday() ? ' Today' : ' ' . $msg->created_at->format('M d')), 'time' => $msg->created_at->format('g:i A') . ($msg->created_at->isToday() ? ' Today' : ' ' . $msg->created_at->format('M d')),
'read_at' => $msg->read_at ? $msg->read_at->toISOString() : null, 'read_at' => $msg->read_at ? $msg->read_at->toISOString() : null,
'created_at' => $msg->created_at->toISOString(), 'created_at' => $msg->created_at->toISOString(),
'attachment_url' => $msg->attachment_path ? asset('storage/' . $msg->attachment_path) : null,
'attachment_type' => $msg->attachment_type,
]; ];
}); });
@ -209,7 +211,8 @@ public function sendMessage(Request $request, $id)
$employer = $request->attributes->get('employer'); $employer = $request->attributes->get('employer');
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'text' => 'required|string|max:1000', 'text' => 'required_without:file|string|max:1000|nullable',
'file' => 'nullable|file|mimes:jpg,jpeg,png,pdf,mp3,wav,m4a,ogg,webm,mp4,aac,3gp,amr|max:10240', // 10MB limit
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@ -232,13 +235,32 @@ public function sendMessage(Request $request, $id)
], 404); ], 404);
} }
$attachmentPath = null;
$attachmentType = null;
if ($request->hasFile('file')) {
$file = $request->file('file');
$attachmentPath = $file->store('chat_attachments', 'public');
$mime = $file->getMimeType();
if (str_starts_with($mime, 'image/')) {
$attachmentType = 'image';
} elseif (str_starts_with($mime, 'audio/')) {
$attachmentType = 'voice';
} else {
$attachmentType = 'document';
}
}
$message = null; $message = null;
DB::transaction(function () use ($conv, $employer, $request, &$message) { DB::transaction(function () use ($conv, $employer, $request, $attachmentPath, $attachmentType, &$message) {
$message = Message::create([ $message = Message::create([
'conversation_id' => $conv->id, 'conversation_id' => $conv->id,
'sender_type' => 'employer', 'sender_type' => 'employer',
'sender_id' => $employer->id, 'sender_id' => $employer->id,
'text' => $request->text, 'text' => $request->text,
'attachment_path' => $attachmentPath,
'attachment_type' => $attachmentType,
]); ]);
// Touch conversation updated_at for sorting // Touch conversation updated_at for sorting
@ -281,6 +303,8 @@ public function sendMessage(Request $request, $id)
'text' => $message->text, 'text' => $message->text,
'time' => $message->created_at->format('g:i A') . ' Today', 'time' => $message->created_at->format('g:i A') . ' Today',
'created_at' => $message->created_at->toISOString(), 'created_at' => $message->created_at->toISOString(),
'attachment_url' => $message->attachment_path ? asset('storage/' . $message->attachment_path) : null,
'attachment_type' => $message->attachment_type,
] ]
] ]
], 201); ], 201);

View File

@ -14,11 +14,15 @@ class Message extends Model
'sender_type', 'sender_type',
'sender_id', 'sender_id',
'text', 'text',
'attachment_path',
'attachment_type',
'is_read', 'is_read',
'read_at',
]; ];
protected $casts = [ protected $casts = [
'is_read' => 'boolean', 'is_read' => 'boolean',
'read_at' => 'datetime',
]; ];
public function conversation() public function conversation()

View File

@ -24,5 +24,10 @@
]); ]);
}) })
->withExceptions(function (Exceptions $exceptions): void { ->withExceptions(function (Exceptions $exceptions): void {
// $exceptions->shouldRenderJsonWhen(function (\Illuminate\Http\Request $request, \Throwable $e) {
if ($request->is('api/*')) {
return true;
}
return $request->expectsJson();
});
})->create(); })->create();

View File

@ -65,7 +65,7 @@
| |
*/ */
'timezone' => 'UTC', 'timezone' => env('APP_TIMEZONE', 'Asia/Dubai'),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->string('attachment_path')->nullable()->after('text');
$table->string('attachment_type')->nullable()->after('attachment_path');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn(['attachment_path', 'attachment_type']);
});
}
};

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->text('text')->nullable()->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->text('text')->nullable(false)->change();
});
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB