I've made this project using the messages panel, and I want to display the message for a specific person. My relations:
In user:
public function Message()
{
return $this->belongsToMany('App\Message');
}
In message:
public function user()
{
return $this->belongsTo('App\User');
}
And in my controller its look like:
public function wiadomosci()
{
$menuClasses = ['', '', '', 'active', ''];
$messages = Message::where('to_id','=', Auth::user()->id);
return view('panel.wiadomosci', compact('menuClasses'))->with('messages', $messages);
}
When I add where in $message
it does not display anything to me, but when I delete it, it shows me all of the messages not assigned to that user.
@@Everything is fine now, problem solved. But I have a new problem: I would like to display the user name who send message(teacher_name) in the view, but I can only display an Id. controller:
public function wiadomosci()
{
$menuClasses = ['', '', '', 'active', ''];
$messages = Message::where('to_id','=', Auth::user()->id)
->join('teachers', 'messages.from_id', '=','teachers.id')
->get();
return view('panel.wiadomosci', compact('menuClasses'))->with('messages', $messages);
}
and in view :
@foreach($messages as $message)
Od:{{$message->teachers_name}}
</br>
Tytuł:{{$message->title}}
</br>
opis:{{$message->contents}}
</br>
@endforeach
In mysql query would look like this :
SELECT teachers.name FROM messages JOIN teachers ON messages.from_id=teachers.id