码迷,mamicode.com
首页 > 其他好文 > 详细

follow

时间:2019-03-21 17:35:10      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:review   cte   column   public   create   _id   edm   trash   ras   

public function follow(Request $request, FeedModel $model, FeedRepository $repository)
    {
        if (is_null($user = $request->user(‘api‘))) {
            abort(401);
        }

        $limit = $request->query(‘limit‘, 15);
        $after = $request->query(‘after‘);
        $feeds = $model->leftJoin(‘user_follow‘, function ($join) use ($user) {
            $join->where(‘user_follow.user_id‘, $user->id);
        })
            ->whereDoesntHave(‘blacks‘, function ($query) use ($user) {
                $query->where(‘user_id‘, $user);
            })
            ->where(function ($query) use ($user) {
                $query->whereColumn(‘feeds.user_id‘, ‘=‘, ‘user_follow.target‘)
                    ->orWhere(‘feeds.user_id‘, $user->id);
            })
            ->with([
                ‘user‘ => function ($query) {
                    return $query->withTrashed();
                },
                ‘topics‘ => function ($query) {
                    return $query->select(‘id‘, ‘name‘);
                },
                ‘user.certification‘,
                ‘pinnedComments‘ => function ($query) {
                    return $query->with([
                        ‘user‘,
                        ‘user.certification‘,
                    ])
                    ->where(‘expires_at‘, ‘>‘, new Carbon)
                    ->orderBy(‘amount‘, ‘desc‘)
                    ->orderBy(‘created_at‘, ‘desc‘);
                },
            ])
            ->when((bool) $after, function ($query) use ($after) {
                return $query->where(‘feeds.id‘, ‘<‘, $after);
            })
            ->distinct()
            ->select(‘feeds.*‘)
            ->orderBy(‘feeds.id‘, ‘desc‘)
            ->limit($limit)
            ->get();

        return $feeds->map(function (FeedModel $feed) use ($repository, $user) {
            $feed->feed_view_count += 1;
            $feed->hot = $feed->makeHotValue();
            $feed->save();

            $repository->setModel($feed);
            $repository->images();
            $repository->format($user->id);
            $repository->previewComments();

            $feed->has_collect = $feed->collected($user->id);
            $feed->has_like = $feed->liked($user);

            return $feed;
        });
    }

follow

标签:review   cte   column   public   create   _id   edm   trash   ras   

原文地址:https://www.cnblogs.com/sgm4231/p/10572810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!