I write small inboxing app in Laravel 5.5 for my wesite. In inbox app users can compose new message, read messages and delete messages. I have only one table in my database for inboxes.
Structure of messages table:
And I have model Messages for this table.
Code: InboxController
<?php
namespace App\Http\Controllers;
use App\User;
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Encryption\DecryptException;
class InboxController extends Controller
{
## Inbox View Control Methods ##
// View for show new messages of current user
public function messages()
{
$array = array
(
'messages'=>$this->user_messages('my_messages'),
'count'=>$this->count_messages()['my_messages'],
'count_sended' => $this->count_messages()['sended_messages'],
);
return view('index', $array);
}
// View for show sended messages of current user
public function sended()
{
$array = array
(
'messages'=>$this->user_messages('my_sended_messages'),
'count'=>$this->count_messages()['my_messages'],
'count_sended' => $this->count_messages()['sended_messages'],
);
return view('sended', $array);
}
// View for show trashed messages of current user
public function trashed()
{
$array = array
(
'messages'=>$this->user_messages('my_sended_messages'),
'count'=>$this->count_messages()['my_messages'],
'count_sended' => $this->count_messages()['sended_messages'],
);
return view('sended', $array);
}
// View for compose and send new message
public function compose(Request $request)
{
if($request->isMethod('post'))
{
// Sended data from html form
$to = $request->to;
$from = Auth::user()->id;
$subject = $request->subject;
$message = $request->message;
// Get info about receiver user
$receiver_user_id = User::where('email', $to)->first()->id;
$receiver_user_type = User::where('id', $receiver_user_id)->first()->type;
// Get info about sender user
$sender_user_type = Auth::user()->type;
// Send message
Message::create(
[
'subject'=>$subject,
'message'=>$message,
'from'=>$from,
'to'=>$receiver_user_id
]
);
}
$count = $this->count_messages()['my_messages'];
$count_sended = $this->count_messages()['sended_messages'];
$array = array
(
'count' => $count,
'count_sended' => $count_sended,
);
return view('compose', $array);
}
// View for read message
public function message(Request $request, $id)
{
try
{
$decrypted = decrypt($id);
}
catch (DecryptException $e)
{
return redirect()->back();
}
$user_id = Auth::user()->id;
$messages = Message::where('id', decrypt($id))->get();
dump($messages);
$message_to_id = $messages->first()->to;
if($user_id == $message_to_id)
{
$subject = $messages->first()->subject;
$message = $messages->first()->message;
$status = $messages->first()->status;
$date = $messages->first()->created_at;
$sender_id = $messages->first()->from;
$sender = User::where('id', $sender_id)->first()->name;
$count = $this->count_messages()['my_messages'];
$count_sended = $this->count_messages()['sended_messages'];
$array = [
'subject' => $subject,
'message' => $message,
'date' => $date,
'messages'=> $messages,
'sender' => $sender,
'status' => $status,
'count' => $count,
'count' => $count,
'count_sended' => $count_sended,
];
if($status == 0 && $sender_id !== $user_id)
{
Message::where('id', decrypt($id))->update(['status' => 1]);
}
return view('message', $array);
}
else return redirect()->back();
}
## Helper functions for get any info from messages and users table ##
public function count_messages()
{
$user_id = Auth::user()->id;
$my_messages = Message::where('to', $user_id)->where('status', 0)->count();
$sended_messages = Message::where('from', $user_id)->count();
$array = ['my_messages' => $my_messages, 'sended_messages' => $sended_messages];
return $array;
}
public function user_messages($status = 'my_messages')
{
$user_id = Auth::user()->id;
switch ($status)
{
case 'my_messages':
$messages = Message::where('to', $user_id)->orderBy('created_at','desc')->get();
break;
case 'my_sended_messages':
$messages = Message::where('from', $user_id)->orderBy('created_at','desc')->get();
break;
default:
$messages = false;
break;
}
return $messages;
}
}
How I can reduce code duplication and beautify my controller code?