2017-06-22 55 views
-1
<?php 

namespace App\Models; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Foundation\Auth\Access\Authorizable; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

class User extends Model implements AuthenticatableContract, 
           AuthorizableContract, 
           CanResetPasswordContract 
{ 
    use Authenticatable, Authorizable, CanResetPassword; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = ['name', 'email', 'password']; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = ['password', 'remember_token']; 

    public static function boot() 
    { 
     parent::boot(); 

     static::creating(function ($user) { 
      $user->activation_token = str_random(30); 
     }); 
    } 

    public function gravatar($size = '100') 
    { 
     $hash = md5(strtolower(trim($this->attributes['email']))); 
     return "http://www.gravatar.com/avatar/$hash?s=$size"; 
    } 

    public function statuses() 
    { 
     return $this->hasMany(Status::class); 
    } 

    public function feed() 
    { 
     return $this->statuses()->orderBy('created_at', 'desc'); 
    } 

    public function followers() 
    { 
     return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id'); 
    } 

    public function followings() 
    { 
     return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id'); 
    } 

    public function follow($user_ids) 
    { 
     if (!is_array($user_ids)) { 
      $user_ids = compact('user_ids'); 
     } 
     $this->followings()->sync($user_ids, false); 
    } 

    public function unfollow($user_ids) 
    { 
     if (!is_array($user_ids)) { 
      $user_ids = compact('user_ids'); 
     } 
     $this->followings()->detach($user_ids); 
    } 

    public function isFollowing($user_id) 
    { 
     var_dump($this->followings);die(); 
     return $this->followings->contains($user_id); 
    } 
} 

这是一个来自laravel模型的代码。laravel代码中的属性来自哪里?

有一个名为$这个 - >以下()。但我的方法不看在代码分配任何$这个 - >以下属性。

哪里是$ this-> followings来自哪里? 感谢

+0

其在模型中定义的关系方法 –

+0

什么样的关系。您能向我展示更多信息 – Tim

回答