Current File : /home/tradevaly/public_html/app/Http/Controllers/User/MessagesController.php
<?php
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\InquiryMessage;
use App\Models\MessageList;
use Carbon\Carbon;
use App\Models\User;
class MessagesController extends Controller
{
    public function index()
    {
        $messages = MessageList::where('person_one',auth()->id())->orWhere('person_two',auth()->id())->latest()->get();
        return view('user.pages.message.messages',compact('messages'));
    }
    public function message($id)
    {
        
        $messages = InquiryMessage::where('chat_id',$id)->orderBy('created_at', 'asc')->get();
        $messageLists = MessageList::where('person_one',auth()->id())->orWhere('person_two',auth()->id())->latest()->get();
        // $sender = MessageList::where('person_one',auth()->id())->orWhere('person_two',auth()->id())->first();
        $sender = MessageList::where('chat_id',$id)->latest()->first();
        $senderId = MessageList::where('chat_id',$id)->latest()->first();
        $senderId->seen = Carbon::now();
        $senderId->save();
        return view('user.pages.message.message',compact('messages','messageLists','sender'));
    }
    
    
    
    public function store(Request $request,$id)
    {
        $message = new InquiryMessage();
        $message->chat_id = $id;
        $message->parent_id = auth()->id();
        $message->message = $request->message;
        $message->type = ($request->message !== null)?'text':'file';
        $message->save();
        
        $messageStatus = MessageList::where('chat_id',$id)->first();
        $messageStatus->last_message = $request->message;
        $messageStatus->seen = null;
        $messageStatus->save();
        return back();
    }
}