【发布时间】:2018-09-20 17:05:13
【问题描述】:
用户可以是追随者和/或领导者。这里 userA 和 userB(都是领导者)在不同的时间被 userC(跟随者)跟随(请参阅下面的 followers 表以获取 created_at 时间戳)。
为了说明问题,我先布局模板:
userX action (year) // template to understand issue below
userC followed userA (2016)
userA added postA/notification (2017)
userC should get notification in feed, since they followed userA a year BEFORE their post
userB added postB/notification (2018)
userC followed userB (2019)
userC should NOT get notification in feed, since they followed userB a year AFTER their post
为此,我尝试了这个查询,但它不能正常工作:
$user = App\User::find(3); // follower
$leaders = $user->leaders()
->with(['leaderNotifications'=>function($query){
$query->where('notifications.created_at','>','followers.created_at');
}])
->get();
我认为该问题与未正确查询 created_at 有关。这是在本地查看的确切设置:
1) 数据库表名/数据
users
followers
notifications
2) 模型
// User
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
class User extends Authenticatable {
use Notifiable;
// Pivot Table
public function leaders() {
return $this->belongsToMany('App\User', ‘followers’, 'follower_id', 'leader_id')
->withPivot('created_at'); // need timestamp to compare against notification timestamp
}
public function leaderNotifications() {
return $this->hasMany(DatabaseNotification::class, 'leader_id')
->orderBy('created_at', 'desc');
}
}
我正在尝试获取当前关注者userC 的正确通知,这意味着只有他们关注领导者之后的新通知,而不是领导者之前的旧通知当前用户紧随其后。
还值得注意的是,最终查询一次应该能够paginate->(20) 这些通知,因为它将用一百万行进行测试,因此我们需要确保它高效/可扩展并且可以分页。
对于这个问题,高效/可扩展的查询是什么?
【问题讨论】:
-
尝试使用
DB::raw('followers.created_at') -
@Ben 你知道如何扩展查询吗?当我有 10k - 1m 行的关注者时,我似乎无法让下面的 Mehran Prs 查询工作并且 php 内存不足,因此它效率不高/不可扩展。我真的不需要领导者()信息(因为我在通知的数据中有它),甚至可能不需要雄辩,但不知道如何使用
DB::raw来做到这一点,其中追随者有 10k - 1m 领导者,并且 1 位领导者有 10k - 100 万个帖子(尝试分页 20)。
标签: laravel laravel-5 eloquent laravel-5.5 eager-loading