MODULES
Internal Messages
What does it do?
This module adds a new section in your project called Messages - where users can
send messages to each other.
Like a mini-inbox.
How does the result look in QuickAdminPanel code?
We create quite a lot of files within module, will mention most important ones.
Migrations for DB tables:
Schema::create('messenger_topics', function (Blueprint $table) { $table->increments('id'); $table->string('subject'); $table->integer('sender_id'); $table->integer('receiver_id'); $table->timestamp('sent_at')->nullable(); $table->timestamp('sender_read_at')->nullable(); $table->timestamp('receiver_read_at')->nullable(); $table->timestamps(); $table->softDeletes(); }); Schema::create('messenger_messages', function (Blueprint $table) { $table->increments('id'); $table->integer('topic_id')->unsigned(); $table->foreign('topic_id')->references('id')->on('messenger_topics')->onDelete('cascade'); $table->integer('sender_id'); $table->text('content'); $table->timestamp('sent_at')->default(DB::raw('CURRENT_TIMESTAMP')); $table->timestamps(); $table->softDeletes(); });MessengerController:
class MessengerController extends Controller { public function index() { $topics = Auth::user()->topics()->with('receiver', 'sender')->orderBy('sent_at', 'desc')->get(); $title = 'Messages'; return view('admin.messenger.index', compact('topics', 'title')); } public function create() { $users = User::all()->pluck('email', 'id'); return view('admin.messenger.create', compact('users')); } public function store(StoreMessageRequest $request) { $sender = Auth::user()->id; MessengerTopic::create([ 'subject' => $request->input('subject'), 'sender_id' => $sender, 'receiver_id' => $request->input('receiver'), 'sent_at' => Carbon::now(), ])->read() ->messages()->create([ 'sender_id' => $sender, 'content' => $request->input('content'), ]); return redirect()->route('admin.messenger.index'); } public function show(MessengerTopic $topic) { $user = Auth::user(); if ($topic->receiver->id != $user->id && $topic->sender->id != $user->id) { return abort(401); } $topic->load('receiver', 'sender', 'messages'); $unreadMessages = []; foreach($topic->messages as $message) { if($message->unread($topic)) { $unreadMessages[] = $message->id; } } $topic->read(); return view('admin.messenger.show', compact('topic', 'unreadMessages')); } public function edit(MessengerTopic $topic) { $user = Auth::user(); if ($topic->receiver->id != $user->id && $topic->sender->id != $user->id) { return abort(401); } $topic->load('receiver', 'sender'); $user = $topic->otherPerson()->email; return view('admin.messenger.reply', compact('topic', 'user')); } public function update(UpdateMessageRequest $request, MessengerTopic $topic) { $user = Auth::user(); if ($topic->receiver->id != $user->id && $topic->sender->id != $user->id) { return abort(401); } $topic->sent_at = Carbon::now(); $topic->save(); $topic->read(); $topic->messages()->create([ 'sender_id' => Auth::user()->id, 'content' => $request->input('content'), ]); return redirect()->route('admin.messenger.show', $topic->id); } public function destroy(MessengerTopic $topic) { $user = Auth::user(); if ($topic->receiver->id != $user->id && $topic->sender->id != $user->id) { return abort(401); } $topic->delete(); return redirect()->route('admin.messenger.index'); } public function inbox() { $topics = Auth::user()->inbox()->with('receiver', 'sender')->orderBy('sent_at', 'desc')->get(); $title = 'Inbox'; return view('admin.messenger.index', compact('topics', 'title')); } public function outbox() { $topics = Auth::user()->outbox()->with('receiver', 'sender')->orderBy('sent_at', 'desc')->get(); $title = 'Outbox'; return view('admin.messenger.index', compact('topics', 'title')); } }And also Blade view files - won't show the code here, just the list:
- resources/views/admin/messenger/create.blade.php
- resources/views/admin/messenger/form-partials/fields.blade.php
- resources/views/admin/messenger/index.blade.php
- resources/views/admin/messenger/reply.blade.php
- resources/views/admin/messenger/show.blade.php
- resources/views/admin/messenger/template.blade.php
You can easily customize the logic after download by adding Laravel code into one of those files.
How to install/use the module?
Go to your panel's Modules menu item, find the module in the list and click Install. You will see a new menu item Messages (1).
And that's it - download your panel and Messages function will be there.