-
app/Http/Controllers/InboxController.php
Open in GitHubuse App\Models\User; use \App\Models\Message; class InboxController extends Controller { public function index() { $users = User::where('is_admin', false)->orderBy('id', 'DESC')->get(); if (auth()->user()->is_admin == false) { $messages = Message::where('user_id', auth()->id())->orWhere('receiver', auth()->id())->orderBy('id', 'DESC')->get(); } return view('home', [ 'users' => $users, 'messages' => $messages ?? null ]); } // }
-
resources/views/home.blade.php
Open in GitHub@extends('layouts.app') @section('content') <div class="container"> @livewire('message', ['users' => $users, 'messages' => $messages ?? null]) </div> @endsection
-
app/Http/Livewire/Message.php
Open in GitHubuse \App\Models\User; use Illuminate\Support\Facades\Storage; use Livewire\Component; use Livewire\WithFileUploads; class Message extends Component { use WithFileUploads; public $message = ''; public $users; public $clicked_user; public $messages; public $file; public $admin; public function render() { return view('livewire.message', [ 'users' => $this->users, 'admin' => $this->admin ]); } public function mount() { if (auth()->user()->is_admin == false) { $this->messages = \App\Models\Message::where('user_id', auth()->id()) ->orWhere('receiver', auth()->id()) ->orderBy('id', 'DESC') ->get(); } else { $this->messages = \App\Models\Message::where('user_id', $this->clicked_user) ->orWhere('receiver', $this->clicked_user) ->orderBy('id', 'DESC') ->get(); } $this->admin = \App\Models\User::where('is_admin', true)->first(); } public function SendMessage() { $new_message = new \App\Models\Message(); $new_message->message = $this->message; $new_message->user_id = auth()->id(); if (!auth()->user()->is_admin == true) { $admin = User::where('is_admin', true)->first(); $this->user_id = $admin->id; } else { $this->user_id = $this->clicked_user->id; } $new_message->receiver = $this->user_id; if ($this->file) { $file = $this->file->store('public/files'); $path = url(Storage::url($file)); $new_message->file = $path; $new_message->file_name = $this->file->getClientOriginalName(); } $new_message->save(); $this->reset(['message']); $this->file = ''; } public function getUser($user_id) { $this->clicked_user = User::find($user_id); $this->messages = \App\Models\Message::where('user_id', $user_id)->get(); } public function resetFile() { $this->reset('file'); } }
-
resources/views/livewire/message.blade.php
Open in GitHub<div> <div class="row justify-content-center" wire:poll="mount"> @if(auth()->user()->is_admin == true) <div class="col-md-4" wire:init> <div class="card"> <div class="card-header"> Users </div> <div class="card-body chatbox p-0"> <ul class="list-group list-group-flush" wire:poll="render"> @foreach($users as $user) @php $not_seen = \App\Models\Message::where('user_id', $user->id)->where('receiver', auth()->id())->where('is_seen', false)->get() ?? null @endphp <a href="{{ route('inbox.show', $user->id) }}" class="text-dark link"> <li class="list-group-item" wire:click="getUser({{ $user->id }})" id="user_{{ $user->id }}"> <img class="img-fluid avatar" src="https://cdn.pixabay.com/photo/2017/06/13/12/53/profile-2398782_1280.png"> @if($user->is_online) <i class="fa fa-circle text-success online-icon"></i> @endif {{ $user->name }} @if(filled($not_seen)) <div class="badge badge-success rounded">{{ $not_seen->count() }}</div> @endif </li> </a> @endforeach </ul> </div> </div> </div> @endif <div class="col-md-8"> <div class="card"> <div class="card-header"> @if(isset($clicked_user)) {{ $clicked_user->name }} @elseif(auth()->user()->is_admin == true) Select a user to see the chat @elseif($admin->is_online) <i class="fa fa-circle text-success"></i> We are online @else Messages @endif </div> <div class="card-body message-box"> @if(!$messages) No messages to show @else @if(isset($messages)) @foreach($messages as $message) <div class="single-message @if($message->user_id !== auth()->id()) received @else sent @endif"> <p class="font-weight-bolder my-0">{{ $message->user->name }}</p> <p class="my-0">{{ $message->message }}</p> @if (isPhoto($message->file)) <div class="w-100 my-2"> <img class="img-fluid rounded" loading="lazy" style="height: 250px" src="{{ $message->file }}"> </div> @elseif (isVideo($message->file)) <div class="w-100 my-2"> <video style="height: 250px" class="img-fluid rounded" controls> <source src="{{ $message->file }}"> </video> </div> @elseif ($message->file) <div class="w-100 my-2"> <a href="{{ $message->file}}" class="bg-light p-2 rounded-pill" target="_blank"><i class="fa fa-download"></i> {{ $message->file_name }} </a> </div> @endif <small class="text-muted w-100">Sent <em>{{ $message->created_at }}</em></small> </div> @endforeach @else No messages to show @endif @if(!isset($clicked_user) and auth()->user()->is_admin == true) Click on a user to see the messages @endif @endif </div> @if(auth()->user()->is_admin == false) <div class="card-footer"> <form wire:submit.prevent="SendMessage" enctype="multipart/form-data"> <div wire:loading wire:target='SendMessage'> Sending message . . . </div> <div wire:loading wire:target="file"> Uploading file . . . </div> @if($file) <div class="mb-2"> You have an uploaded file <button type="button" wire:click="resetFile" class="btn btn-danger btn-sm"><i class="fa fa-times"></i> Remove {{ $file->getClientOriginalName() }}</button> </div> @else No file is uploaded. @endif <div class="row"> <div class="col-md-7"> <input wire:model="message" class="form-control input shadow-none w-100 d-inline-block" placeholder="Type a message" @if(!$file) required @endif> </div> @if(empty($file)) <div class="col-md-1"> <button type="button" class="border" id="file-area"> <label> <i class="fa fa-file-upload"></i> <input type="file" wire:model="file"> </label> </button> </div> @endif <div class="col-md-4"> <button class="btn btn-primary d-inline-block w-100"><i class="far fa-paper-plane"></i> Send</button> </div> </div> </form> </div> @endif </div> </div> </div> </div>