I'm trying to set up a simple private messaging system with laravel. Kind of what we have on forum where you can send a private message to a user. Following this stackoverflow post -> Conversation, Many-to-Many relationship I got to a point but I think I'm missing something..
I made the tables as suggested in that post and also the methods from the Models. So, exactly the same. In order to send a message to a user I'm using this method in my controller (I think the way I'm saving this, might be the problem)
//Routes
Route::post('/browse/profile/{id}/message', 'MessageController@store');
Route::get('/profile/messages','MessageController@show')->name('mymessages');
//Store method
public function store(Request $request, $id)
{
$this->validate($request, [
'title' => 'required|min:5|max:100',
'message' => 'required|min:5'
]);
$conversation = new Conversation;
$message = new Message;
$userID = $id;
$conversation->title = $request->input('title');
$message->user_id = $userID;
$message->conversation_id = Auth::id();
$message->message = $request->input('message');
$message->save();
$conversation->save();
return redirect()->back();
}
Here I'm using a form to get the data and I'm setting the action of the form with jquery. So the $userID is the id of the user that I want to send the message to.
//Show method
public function show(Message $message)
{
$userID = Auth::user()->id;
$conversations = User::find($userID)->conversations;
return $conversations;
}
As you can see in the show method I'm just returning $conversations and unfortunately something is not working because I'm just getting an empty array.. I feel like I didn't understand properly the way this tables are structured.. Any suggestions? Thanks!
via Ovidiu G