where('videos.created_at', '>', '2020.07.26 14:16:29') 不适用于左连接 table - Eloquent, Laravel
where('videos.created_at', '>', '2020.07.26 14:16:29') not working for left joined table - Eloquent, Laravel
各位。
我写了这段代码,它应该 return 最喜欢的视频 post 在指定日期之后。但是代码 returns 比指定日期早的结果。在 2020.07.26 之后,我只 post 编辑了一个视频,尽管如此 Eloquent return 还是 8 个旧视频。
$query = Video::select('videos.id',DB::raw('group_concat(DISTINCT videos.code) AS code'),DB::raw('group_concat(DISTINCT videos.title) AS title'),DB::raw('group_concat(DISTINCT videos.created_at) AS createdDate'),DB::raw('count(reactions.id) AS reactCount'))
->leftJoin('reactions','reactions.at_video','=','videos.id')
->where('reactions.reaction',1)
->orWhereNull('reactions.reaction')
->where('videos.created_at', '>', '2020.07.26 14:16:29')
->groupBy('videos.id')
->orderBy('reactCount','DESC')
->get();
dd($query);
然后我在解决方案 whereColumn
的存货流中找到了这段代码。但这对我没有帮助。 Laravel 显示另一个错误。
whereColumn 来自另一个 post on Whosebug
的示例
$query = DB::table('messages')
->leftJoin('participants', function ($join) {
$join->on('messages.thread_id', '=', 'participants.thread_id')
->on('messages.user_id', '!=', 'participants.user_id');
})
->select('messages.created_at as message_date', 'participants.last_read as last_read')
->whereColumn('messages.created_at', '>', 'participants.last_read')->get();
这样做的原因是我对结果进行了分组。 videos.created
字段 return 的值与视频的点赞次数一样多。为了解决这个问题,我使用了 DISTINCT
DB::raw('group_concat(DISTINCT videos.created_at) AS createdDate')
然后我在 WHERE()
中添加了 createdDate
不幸的是我又遇到了另一个错误。
->where('createdDate', '>', '2020.07.26 14:16:29')
这就是我的意思。我得到了很多次……我几乎爱上了它;)
还有...
我写了一个查询代码,它 return 的正确结果和 运行 在本机 php 页面中。
这是Laravel应该做的,但它没有...
SELECT videos.id AS id, group_concat(videos.code) AS code, group_concat(videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2020.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC
我想我会在Laravel中使用自定义查询代码作为最终解决方案但是我不得不说但是一个再来一次。
$videos = DB::select("SELECT videos.id AS id, group_concat(videos.code) AS code, group_concat(videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2020.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC")->paginate(16);
请有人帮我找到解决办法。我真的很累伙计。
您尝试过使用 whereDate()
吗?
->whereDate('videos.created_at', '>', '2020.07.27');
或者您也许应该使用 HasMany 关系来获取反应计数。
$date = Carbon::createFromFormat('Y-m-d', '2020-07-27');
在视频模型中创建 HasMany 关系
public function reactions(){
$this->hasMany(Reaction::class, 'at_video', 'id');
}
创建Eloquent查询以获取记录
Video::select('id', 'code', 'title')
->withCount('reactions')
->whereDate('videos.created_at', '>', $date)
->get();
至少我找到了解决办法。这可能不是最好的方法,但它可以正常工作。
我将它们添加到我的控制器
use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
函数中
$query = DB::select(DB::raw("SELECT videos.id AS id, group_concat(DISTINCT videos.code) AS code, group_concat(DISTINCT videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2021.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC"));
// Added this three lines of code to make as the results array as a collection
$collection = new Collection($query);
$page = $request->get('page') ? (int)$request->get('page') : 1;
$videos = $collection->forPage($page, 16);
dd($videos);
各位。 我写了这段代码,它应该 return 最喜欢的视频 post 在指定日期之后。但是代码 returns 比指定日期早的结果。在 2020.07.26 之后,我只 post 编辑了一个视频,尽管如此 Eloquent return 还是 8 个旧视频。
$query = Video::select('videos.id',DB::raw('group_concat(DISTINCT videos.code) AS code'),DB::raw('group_concat(DISTINCT videos.title) AS title'),DB::raw('group_concat(DISTINCT videos.created_at) AS createdDate'),DB::raw('count(reactions.id) AS reactCount'))
->leftJoin('reactions','reactions.at_video','=','videos.id')
->where('reactions.reaction',1)
->orWhereNull('reactions.reaction')
->where('videos.created_at', '>', '2020.07.26 14:16:29')
->groupBy('videos.id')
->orderBy('reactCount','DESC')
->get();
dd($query);
然后我在解决方案 whereColumn
的存货流中找到了这段代码。但这对我没有帮助。 Laravel 显示另一个错误。
whereColumn 来自另一个 post on Whosebug
的示例$query = DB::table('messages')
->leftJoin('participants', function ($join) {
$join->on('messages.thread_id', '=', 'participants.thread_id')
->on('messages.user_id', '!=', 'participants.user_id');
})
->select('messages.created_at as message_date', 'participants.last_read as last_read')
->whereColumn('messages.created_at', '>', 'participants.last_read')->get();
这样做的原因是我对结果进行了分组。 videos.created
字段 return 的值与视频的点赞次数一样多。为了解决这个问题,我使用了 DISTINCT
DB::raw('group_concat(DISTINCT videos.created_at) AS createdDate')
然后我在 WHERE()
中添加了 createdDate
不幸的是我又遇到了另一个错误。
->where('createdDate', '>', '2020.07.26 14:16:29')
这就是我的意思。我得到了很多次……我几乎爱上了它;)
还有... 我写了一个查询代码,它 return 的正确结果和 运行 在本机 php 页面中。 这是Laravel应该做的,但它没有...
SELECT videos.id AS id, group_concat(videos.code) AS code, group_concat(videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2020.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC
我想我会在Laravel中使用自定义查询代码作为最终解决方案但是我不得不说但是一个再来一次。
$videos = DB::select("SELECT videos.id AS id, group_concat(videos.code) AS code, group_concat(videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2020.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC")->paginate(16);
请有人帮我找到解决办法。我真的很累伙计。
您尝试过使用 whereDate()
吗?
->whereDate('videos.created_at', '>', '2020.07.27');
或者您也许应该使用 HasMany 关系来获取反应计数。
$date = Carbon::createFromFormat('Y-m-d', '2020-07-27');
在视频模型中创建 HasMany 关系
public function reactions(){ $this->hasMany(Reaction::class, 'at_video', 'id'); }
创建Eloquent查询以获取记录
Video::select('id', 'code', 'title') ->withCount('reactions') ->whereDate('videos.created_at', '>', $date) ->get();
至少我找到了解决办法。这可能不是最好的方法,但它可以正常工作。
我将它们添加到我的控制器
use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
函数中
$query = DB::select(DB::raw("SELECT videos.id AS id, group_concat(DISTINCT videos.code) AS code, group_concat(DISTINCT videos.title) AS title, group_concat(DISTINCT videos.created_at) AS createdDate, count(reactions.id) AS reactCount
FROM videos
LEFT JOIN reactions ON videos.id = reactions.at_video
WHERE (reactions.reaction = 1 or reactions.reaction IS NULL)
AND videos.created_at > '2021.07.27'
GROUP BY videos.id
ORDER BY reactCount DESC"));
// Added this three lines of code to make as the results array as a collection
$collection = new Collection($query);
$page = $request->get('page') ? (int)$request->get('page') : 1;
$videos = $collection->forPage($page, 16);
dd($videos);