总是回归相同的关系

时间:2018-03-11 12:27:10

标签: laravel eloquent

即使类型为1或2,返回关系'user_share'...我尝试使用===但是= =每次都不返回任何关系......

我有Feed.php模型:

public function page_link()
{
    return $this->belongsTo(Link::class, 'page_id', 'user_id')->inRandomOrder()->Select('links.id', 'links.title', 'links.photo', 'links.country', 'links.friendly_url', 'links.clicks', 'links.description', 'links.suggestions', 'links.count_comments', 'links.url', 'links.shares', 'links.page_id', 'links.tag_id', 'links.created_at')->where('sponsored', 0)->where('scheduled', 0)>where('status', 1)->take(3)->orderBy('id','desc');
}

public function user_share()
{
    return $this->belongsTo(Share::class, 'user_id', 'user_id')->inRandomOrder()->Select('id', 'link_id', 'user_id', 'shared_in', 'content', 'created_at')->take(3)->orderBy('id', 'desc')->where('type', '=', 0);
}

public function user(){
    return $this->belongsTo(User::class, 'user_id', 'id');
}

     public function scopeProfile($query) {
        return $query
                        ->when($this->type == 0, function($q) {
                            return $q->with('user_share.link.page')->with('user')->with('user_share.link.tag')->with('user_share.link.comments.user');
                        })
                        ->when($this->type == 1, function($q) {
                            return $q->with('page_link');
                        })
                        ->when($this->type == 2, function($q) {
                            return $q->with('group_link');
                        });
    }

Controller.php这样:

$feed = Feed::Profile()
        ->where('follower_id', auth()->user()->id)
        ->take(10)
        ->get();

可能会发生什么?

0 个答案:

没有答案
相关问题