2017-06-22 13 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->以下属性が表示されていないます$ this->以下の() .Butという名前のメソッドがあります。

ここでは$ this-> followingsが由来しますか? おかげ特に

+0

モデルに定義されている関係メソッド –

+0

どのような種類の関係 – Tim

答えて

3

Suggested reading about Laravel model relationships

:関係が定義されると

、我々は雄弁の動的なプロパティを使用して、関連するレコードを検索することができます。動的プロパティを使用すると、モデルに定義されているプロパティのように関係メソッドにアクセスできます

関連する問題