我正在创建一个收件箱系统,我希望每次对话都获得最新消息。
消息表
id sender_id sent_to_id body created_at updated_at
1 2 1 hello 2019-06-01 20:20:01
2 1 2 ok 2019-06-02 23:20:01
3 3 1 yes 2019-06-01 17:20:01
结果应如下所示:
2 1 2 ok 2019-06-02 23:20:01
3 3 1 yes 2019-06-01 17:20:01
Message Model
public function sender()
{
return $this->belongsTo(User::class, 'sender_id');
}
public function receiver()
{
return $this->belongsTo(User::class, 'sent_to_id');
}
答案 0 :(得分:0)
从您的消息模型中,您可以像这样获得最后一条消息:
#include <windows.h>
#include <cstdint>
#include <string>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
int main() {
HINSTANCE hinstance;
HWND hwnd;
hinstance = GetModuleHandle(0);
WNDCLASSEX wc{};
wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hinstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "test\0";
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wc);
hwnd = CreateWindowEx(0, "test\0", "test\0", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
0, 0, hinstance, 0);
ShowWindow(hwnd, SW_SHOW);
MSG msg{};
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
如果您想直接在表格上做
$latestMessage= Message::latest()->first();
如果您只想从特定发件人的邮件中提取邮件,
$latestMessage= DB::table('messages')
->latest()
->first();
无论哪种方式,您都有一个对象,该对象包含了您在问题中所要查询的所有数据-然后,您可以按照自己想要的任何方式输出它:
$latestMessage= Message::where('sender_id', $someSenderIdVar)->latest()->first();
等等将其移到刀片文件中,现在有了对象并得到所需的结果,就可以很容易地制作一张桌子。