Laravel 日期比较不适用于 created_at 列
Laravel date comparasion not working for created_at column
我编写了一个查询,用于检查在对话中的用户删除对话后写入对话的消息。这是我的查询:
$messages = DB::table('conversations')
->where('conversations.id', $conversation->id)
->join('messages', 'messages.conversation_id', '=', 'conversations.id')
->join('deleted_conversations', 'deleted_conversations.conversation_id', '=', 'conversations.id')
->where('messages.created_at', '>=', 'deleted_conversations.created_at')
->orderBy('messages.created_at', 'desc')
->select('messages.*')
->get()
->all();
但每次我尝试 运行 使用 where('messages.created_at, '<', 'deleted_conversations_created_at
语句进行此查询时,如果不准确检查日期。为什么?
这是迁移:
Schema::create('conversations', function (Blueprint $table) {
$table->id();
$table->integer('sender_id');
$table->integer('receiver_id');
$table->enum('status', ['read', 'unread']);
$table->timestamps();
});
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('sender_id');
$table->string('message');
$table->timestamps();
});
Schema::create('deleted_conversations', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
});
deleted_conversations table
id
conversation_id
user_id
created_at
updated_at
1
1
2
2021-02-28 11:26:36
2021-02-28 11:30:45
conversations table
id
sender_id
user_id
receiver_id
status
created_at
updated_at
1
1
1
2
unread
2021-02-28 11:07:09
2021-02-28 11:31:50
messages table
id
conversation_id
sender_id
message
created_at
updated_at
1
1
1
S.A
2021-02-28 00:00:00
2021-02-28 00:00:00
2
1
2
A.S
2021-02-28 00:00:00
2021-02-28 00:00:00
当你想添加一个值为另一列的 where
子句时,你需要使用 whereColumn():
->whereColumn('messages.created_at', '>=', 'deleted_conversations.created_at')
我编写了一个查询,用于检查在对话中的用户删除对话后写入对话的消息。这是我的查询:
$messages = DB::table('conversations')
->where('conversations.id', $conversation->id)
->join('messages', 'messages.conversation_id', '=', 'conversations.id')
->join('deleted_conversations', 'deleted_conversations.conversation_id', '=', 'conversations.id')
->where('messages.created_at', '>=', 'deleted_conversations.created_at')
->orderBy('messages.created_at', 'desc')
->select('messages.*')
->get()
->all();
但每次我尝试 运行 使用 where('messages.created_at, '<', 'deleted_conversations_created_at
语句进行此查询时,如果不准确检查日期。为什么?
这是迁移:
Schema::create('conversations', function (Blueprint $table) {
$table->id();
$table->integer('sender_id');
$table->integer('receiver_id');
$table->enum('status', ['read', 'unread']);
$table->timestamps();
});
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('sender_id');
$table->string('message');
$table->timestamps();
});
Schema::create('deleted_conversations', function (Blueprint $table) {
$table->id();
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
});
deleted_conversations table
id | conversation_id | user_id | created_at | updated_at |
---|---|---|---|---|
1 | 1 | 2 | 2021-02-28 11:26:36 | 2021-02-28 11:30:45 |
conversations table
id | sender_id | user_id | receiver_id | status | created_at | updated_at |
---|---|---|---|---|---|---|
1 | 1 | 1 | 2 | unread | 2021-02-28 11:07:09 | 2021-02-28 11:31:50 |
messages table
id | conversation_id | sender_id | message | created_at | updated_at |
---|---|---|---|---|---|
1 | 1 | 1 | S.A | 2021-02-28 00:00:00 | 2021-02-28 00:00:00 |
2 | 1 | 2 | A.S | 2021-02-28 00:00:00 | 2021-02-28 00:00:00 |
当你想添加一个值为另一列的 where
子句时,你需要使用 whereColumn():
->whereColumn('messages.created_at', '>=', 'deleted_conversations.created_at')