【发布时间】:2017-02-09 19:24:41
【问题描述】:
所以,我正在尝试建立一种用户可以关注其他用户或关注类别的关系。 我的直觉告诉我,到目前为止我所做的并不是正确的做事方式。我对如何创建 follower - followee 关系感到特别困惑。
表格:
用户
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('password');
$table->string('first_name');
});
}
类别
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('category');
});
}
关注
public function up()
{
Schema::create('follows', function (Blueprint $table) {
$table->increments('id');
$table->integer('follower_id');
$table->integer('followee_id')->nullable();
$table->integer('category_id')->nullable();
});
}
型号:
用户
class User extends Model implements Authenticatable
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
类别
class Category extends Model
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
关注
class Follow extends Model
{
public function post()
{
return $this->belongsTo('App\User');
}
public function source()
{
return $this->belongsTo('App\Category');
}
}
【问题讨论】: