简体   繁体   English

Laravel 5.5-多态关系

[英]Laravel 5.5 - Polymorphic relationships

Let's say I have four tables (models) in Laravel: 假设我在Laravel中有四个表(模型):

User.php user.php的

id - integer
name - string

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'token_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function embeds()
    {
        return $this->hasManyThrough('App\Post', 'App\Role');
    }
}

UserRoles.php UserRoles.php

id - integer
user_id - integer
role_id - integer

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class UserRoles extends Pivot
{
    protected $guarded = [];

    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function user()
    {
        return $this->belongsToMany(User::class, 'user_id');
    }
}

Role.php Role.php

id - integer
name - string

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    /**
     * Don't auto-apply mass assignment protection.
     *
     * @var array
     */
    protected $guarded = [];

    public function roles()
    {
        return $this->belongsToMany(UserRoles::class);
    }
}

Post.php post.php中

id - integer
title - string
body - text
role_id - integer

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Don't auto-apply mass assignment protection.
     *
     * @var array
     */
    protected $guarded = [];
}

As you can see, a UserRoles is a pivot table to connect Users with Roles . 如您所见, UserRoles是用于将UsersRoles连接的数据透视表。 And Posts can only be associated with one role (tech spec req). 并且Posts只能与一个角色相关联(技术规格要求)。

How would make the relationships in Eloquent make it so I can have a user get every single Post they can see through the Roles they are assigned? 如何在Eloquent中建立关系,使我能够让用户获得他们可以通过分配的Roles看到的每个Post

I have revised looking through the Laravel Relationships documentation and I have seen Many-to-Many polymorphic relationships but it seems I keep getting: SQLSTATE[42S02]: Base table or view not found: 1146 Table or some sort. 我已经修改了Laravel Relationships文档,并且看到了Many-to-Many多态关系,但是似乎我不断得到: SQLSTATE[42S02]: Base table or view not found: 1146 Table或某种形式。

What am I doing wrong? 我究竟做错了什么?

Well, a pivot table does need a model. 数据透视表确实需要一个模型。 You need four tables and three Models. 您需要四个表和三个模型。

You almost got the tables right. 您几乎把桌子弄对了。 Just rename the UserRole table to role_user to follow Laravel convention . 只需将UserRole表重命名为role_user即可遵循Laravel约定

The role_user table is derived from the alphabetical order of the related model names, and contains the user_id and role_id columns. role_user表是从相关模型名称的字母顺序得出的,并且包含user_id和role_id列。

And here is how you should set up the models: 这是您应该如何设置模型的方法:

// User.php

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public function posts()
    {
        return $this->hasManyThrough(Post::class, Role::class);
    }

// Role.php

    public function users()
    {
        return $this->belongsToMany(User::class);
    }


    public function posts()
    {
        return $this->hasMany(Post::class);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM