我创建系统消息,我有一个问题,当我有用户发送消息和用户获取消息。无论谁发送和收到谁以及这些消息有多少,如何返回一条消息?我想要创建列表消息。我想创建一个消息列表,其中最后一条消息显示在列表中,点击它时将是整个对话
public function mymessages()
{
$user= Auth::user()->id;
$messages = Messages::latest()->where('id_user_to_send',$user)->orWhere('id_user_from',$user)->get();
return $messages;
return view('messages.inbox', compact('messages'));
}
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('id_user_to_send')->unsigned();
$table->integer('id_user_from')->unsigned();
$table->string('text');
$table->string('title');
$table->foreign('id_user_to_send')->references('id')->on('users');
$table->foreign('id_user_from')->references('id')->on('users');
$table->timestamps();
});
}
答案 0 :(得分:0)
使用first()
,您会得到一个结果:
Messages::where('id_user_to_send',$user)
->orWhere('id_user_from',$user)
->orderBY('id', 'DESC')
->groupBY('id_conversation')
->first();