Refactor chat functionality: remove ChatController, enhance MessageSent event with broadcasting capabilities, and update Conversation and Message models for improved relationships. Update API routes for chat operations and adjust CORS settings for broadcasting authentication.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Chat\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ConversationResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$otherUser = $this->relationLoaded('users')
|
||||
? $this->users->firstWhere('id', '!=', $request->user()?->id)
|
||||
: null;
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'user' => $this->when($otherUser, fn () => new ChatUserResource($otherUser)),
|
||||
'last_message' => $this->when(
|
||||
$this->relationLoaded('latestMessage') && $this->latestMessage,
|
||||
fn () => new MessageResource($this->latestMessage)
|
||||
),
|
||||
'unread_count' => (int) ($this->unread_count ?? 0),
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user