如果关注页面,请获取

时间:2018-01-14 15:56:14

标签: laravel eloquent

如果用户正在关注某个网页,我会尝试获取,但返回空...

在关系中返回空:

DECLARE SQCURS CURSOR  
FOR SELECT (SELECT NEXTVAL AS SQ FROM OPENQUERY(MYSERVER, 
'SELECT ORCL.NAME_SEQNO.NEXTVAL FROM DUAL')), 
 psn.BirthDate, psn.FirstName, psn.MiddleName, psn.LastName, c.REGION_CODE

DD($用户);

public function get_following()
{
    return $this->belongsTo(Followers::class, 'user_id')->where('type', '=', 0)->where('follower_id', '=', auth()->user()->id);
}

此代码返回结果:

UserProfile {#301 ▼
  #table: "users_profile"
  #relations: array:3 [▼
    "user" => User {#305 ▶}
    "shares" => Collection {#307 ▶}
    "get_following" => null
  ]

返回:

$get_follow = Followers::where('type', '=', 0)->where('follower_id', '=', auth()->user()->id)->where('user_id', '=', 1)->first();

dd($get_follow);

1 个答案:

答案 0 :(得分:0)

首先,尝试更好地命名关系,它可以帮助你更好地完成主题

class UserProfile extends Model
{
   public function user()
   {
        return $this->belongsTo(User::class);
   }

   public function followers()
   {
     return $this->user()->followers();
   }
}

class User extends Model
{   

   Public function followers()
   {
     return $this hasMany(Follower::class);
   }
}

Class Follower extends Model
{
  public function user()
  {
     return $this->belongsTo(User::class);
  }
}

现在你如何从一个peofile获得粉丝?

$profile->followers