Hello everybody,
I am trying to construct a small follower system with Laravel 5. The principle is simple, I have many users and each user can follow another user. I would like to use Laravel's relationships but my mind blows a little...
I have the following migration for the moment:
public function up()
{
Schema::create('followers', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('follows')->unsigned();
$table->timestamps();
});
}
The problem is that I would like, in facts, have 2 foreign keys, because a user referenced by the 'user_id' field has also another user_id to follow. Here, the user_id should reference the follower and the "follows" field references the followed member. But I am not quite sure on names, and I think that these are really bad.
But anyway, next, I have a problem to figure out the organization. Because in my App\User model, il would like to have the following function:
public function followers(){
return $this->hasMany('App\Follower');
}
But I also would love to have a following() function listing all the members that a user follows to use something like
$user->following();
But I really don't know which relation to use..
I hope that you will be able to help me or guide me,
Thank you very much in advance,
Mathieu