简体   繁体   English

在laravel 5.1中以高效的方式吸引关注者

[英]Get the followers on a post efficient way in laravel 5.1

I have build a follower system using Eloquent for API (thanks to John Bupit ) but I am getting very slow query (n+1) , its taking 5 second for response, here is the way I have done it. 我已经使用Eloquent for API构建了一个跟踪器系统(感谢John Bupit ),但是查询速度非常慢(n+1) ,它需要5秒钟的响应时间, 就是我的方法。

I need to know is current user following current post, so I have appended the id of all users following on every post (which is making a lot of sql queries). 我需要知道当前帖子之后的当前用户,所以我在每个帖子后都附加了所有用户的id (这使很多SQL查询成为可能)。

Post Model 邮政模型

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['followers'];

/**
 * Get the list of followers of this post.
 *
 * @return bool
 */
public function getFollowersAttribute()
{
    return $this->followers()->get()->lists('id');
}

As you can see I have added an attribute followers on model which fetches the lists of ids user following. 如您所见,我在模型上添加了一个属性followers ,该属性获取了用户关注的ids列表。

Is there any other way I can get the following state of post for user, How can I make it faster. 还有什么其他方法可以让用户获得以下发布状态,如何使其更快。

I want to show Following if user is following the post with a count of total followers. 我想显示“关注中”(如果用户正在关注该帖子,并且关注者总数)。

I am returning a paginated list of 20 posts per page from API as json. 我从json返回每页20个帖子的分页列表。

Update 更新

I have tried eager loading and its fast now, but how can I get only the list of user ids, not hole user model. 我尝试过立即加载并快速加载,但是如何只获取用户ID列表,而不获取用户模型。 here is my code 这是我的代码

$post->with([
  'followers' => function($q) {
      $q->select('user_id'); // select id is giving Column 'id' in field list is ambiguous
},

and its giving me 它给了我

followers: [
{
    user_id: 32
},
{
    user_id: 3
},
{
    user_id: 21
},
{
    user_id: 33
},
{
    user_id: 46
},
{
    user_id: 30
}

]

I want something like 我想要类似的东西

followers : [45,2,45,87,12] //as array of ids

it will cause problem, for example if a post get 1000 follower, I bet it will be slow again, and I cant limit the eager loaded result 它将导致问题,例如,如果某个帖子获得1000个关注者,我敢打赌它会再次变慢,并且我无法限制eager loaded结果

$q->select('user_id')->limit(20); // Limit doesn't work

Other Option 其他选择

One another way will be to cache number of followers on posts table like followers_count and then some way I can check that logged in user is following the each post using a flag ex. 另一种方式将是跟随者缓存数量上的职位表像followers_count ,然后一些方法可以检查用户登录这是继使用标志前的每个职位。 is_following . is_following I dont know I am lost here. 我不知道我在这里迷路了。

Please help guys, how twitter, facebook has done it? 请大家帮忙,twitter,facebook是如何做到的? to get the followers on a post and is current user following given post. 获得关注者的帖子,并且当前用户是给定帖子的当前用户。

You may use something like array_reduce after you get followers . 获得followers后,可以使用array_reduce之类的方法

Example: 例:

$followers = array_reduce($post->followers, function ($c, $i) {
    $c[] = $i->user_id;
}, []);

Now the $followers contain all follower id s. 现在, $followers包含所有关注者id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM