如何使用多形态创建跟随关系与Laravel的多对多关系? [英] How to create follower relationships using Polymorphic Many to Many relationships with Laravel?

查看:198
本文介绍了如何使用多形态创建跟随关系与Laravel的多对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图创建一个关系,用户可以跟随其他用户或跟随类别。我建议使用 Polomorpic Many to Many 关系。现在我正在尝试创建一个追踪者 - 跟随者的关系,但是我无法如何设置模型和控制器。

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

用户

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

类别

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

关注

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

模型

用户

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;
    }
}

可追踪

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.

控制器

FollowableController

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]);
    }

}

说实话我是有点被我创建的代码困惑。没有了解我所做的100%的一切,这是一些复制和粘贴。无论如何,这将在数据库中创建两个记录,如下所示:

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:

可追踪

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

followable_id是错误,似乎使用Id创建在可跟随表中,而不是User表中的user_id。

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.

所以,如何解决,只有只有一个记录被保存到数据库,使用 right followable_id

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

推荐答案

我猜你使用的是错误的方法。我按照Laravel的文档示例做了类似的操作,而 morphMany()已经足够了,因为你的 User 可以有很多关注者,它们本身是用户,所以你可以有:

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:

用户 model:

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

然后您可以从控制器设置user_id(但不要忘记添加到可填充)。

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

希望它有帮助:)

这篇关于如何使用多形态创建跟随关系与Laravel的多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆