Skip to content

Commit

Permalink
Messages working "live"
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyNadd committed Nov 18, 2024
1 parent 09a5dd5 commit 384c040
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 348 deletions.
32 changes: 29 additions & 3 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\Http\Controllers;
use App\Models\Message;

use Illuminate\Http\Request;

Expand All @@ -9,9 +10,11 @@ class ChatController extends Controller
public function index()
{
$discussions = auth()->user()->chats()
->with(['messages' => function ($query) {
$query->latest()->limit(1);
}])
->with([
'messages' => function ($query) {
$query->latest()->limit(1);
}
])
->latest()
->get();

Expand All @@ -20,4 +23,27 @@ public function index()
'selectedDiscussion' => $discussions->first(),
]);
}

public function getMessages($chatId)
{
$messages = Message::where('chat_id', $chatId)
->orderBy('created_at', 'asc')
->get();

return response()->json(['messages' => $messages]);
}

public function storeMessage(Request $request, $chatId)
{
$request->validate([
'message' => 'required|string',
]);
$message = new Message();
$message->user_id = auth()->id();
$message->chat_id = $chatId;
$message->message = $request->message;
$message->save();

return response()->json(['message' => $message]);
}
}
2 changes: 1 addition & 1 deletion app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Message extends Model
protected $table = 'messages';

protected $fillable = [
'content',
'message',
'chat_id',
'user_id',
];
Expand Down
Loading

0 comments on commit 384c040

Please sign in to comment.