Please, say me how can i see the last message of each dialog on the single page, named dialogs.
i have two tables: users and chats.
users - standart table with standart meanings.
chats - have fields: user_id, friend_id, chat. user_id and friend_id - there are users who have a conversation, chat - one message from one of them:
id|---user_id---|---friend_id---|---chat---
1 |---1---------|---2-----------|--this is the first message
2 |---2---------|---1-----------|--this is the second message
This part of controller ChatController
public function index()
{
$users = User::all();
$user = Auth::user();
$last_message = Chat::select(['chat'])->where('user_id', $user->id)->orWhere('friend_id', $user->id)->orderBy('id', 'desc')->first(); - //this code not corrected, it display the message from either user
$search_string = Input::get('search-tag');
$search_result = null;
if(!empty($search_string)){
$search_result = User::where('name', 'like', '%' . $search_string . '%')
->orWhere('name', 'like', '%' . strtolower($search_string) . '%')
->orWhere('name', 'like', '%' . strtoupper($search_string) . '%')
->orWhere('id', strtoupper($search_string))->get();
}
$friends = Auth::user()->friends();
return view('chat.index')
->with('user', $user)
->with('friends', $friends)
->with('search_result', $search_result)
->with('last_message', $last_message)
->with('users', $users);
}
This is the model of chat:
<?php
class Chat extends Model
{
protected $dispatchesEvents = [
'created' => BroadcastChat::class
];
protected $fillable = ['user_id', 'friend_id', 'chat'];
public function user(){
return $this->belongsTo(User::class);
}
public function friend(){
return $this->belongsTo(User::class);
}
}
I need in correct declaration user_id and friend_id to output a message between these specific users to the variable $last_message and display it in the chats.index
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire