How to create follower relationships using Polymorphic Many to Many relationships with Laravel?

▼魔方 西西 提交于 2020-01-17 04:12:25

问题


Okay, so I'm trying to create a relationship where users can follow other users or follow categories. I got a recommendation to use Polomorpic Many to Many relationships. Right now I'm trying to create a follower - followee relationship but I can't wrap my head around how to set up the models and the controller.

TABLES

Users

        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->string('password');
            $table->string('first_name');
        });

Categories

        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('category');
        });

Followables

        Schema::create('followables', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('user_id');
            $table->integer('followable_id');
            $table->string('followable_type');
        });

MODELS

User

class User extends Model implements Authenticatable 
{

    public function followers()
    {
        return $this->morphToMany('App\Followable', 'followable');
    }

    public function following()
    {
        return $this->morphedByMany('App\Followable', 'followable');
    }

    //Use to get id from Auth facade
    public function getId()
    {
        return $this->id;
    }
}

Followable

class Followable extends Model
{

    protected $fillable = ['user_id'];

    public function followable()
    {
        return $this->morphTo();
    }

}

The Category model follower relationship does not exist yet so I will leave that one out for now.

CONTROLLER

FollowableController

class FollowableController extends Controller
{

    public function followUser($followable_id){

        $user_id = Auth::user()->getId();
        $followee = User::find($followable_id);
        $followee->followers()->create(['user_id' => $user_id]);


        return redirect()->route('get.user', ['user_id' => $followable_id]);
    }

}

To be honest I'm a bit confused by the code I've created. It's been some copy and pasting without understanding 100 % of what I do. Anyways, this will create two records in the database looking like this:

Followable

-------------------------------------------------
|Id | user_id | followable_id | followable_type |
-------------------------------------------------
|22  | 4       | NULL          | NULL            |
-------------------------------------------------
|23  | 0       | 22            | App\User        |
-------------------------------------------------

The followable_id is wrong and seems to use the Id which is created in the Followable table instead of the user_id from the User table.

So, how to solve so that there is only one record being saved to the database with the right followable_id?


回答1:


I guess you are using the wrong method. I did something like that following Laravel's doc example, and morphMany() was sufficient, since your User can have many followers, which are themselves user so you can have:

User model:

public function followers()
{
    return $this->morphMany('App\Followable', 'followable');
}

Then you can set the user_id from the controller (but don't forget to add it to fillable).

Hope it helps :)



来源:https://stackoverflow.com/questions/42205928/how-to-create-follower-relationships-using-polymorphic-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!