Php Laravel口若悬河的多对多如何添加到哪里?

Php Laravel口若悬河的多对多如何添加到哪里?,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有三个定义如下的表: CREATE TABLE `roles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`), KEY `roles_title_index` (`title`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=ut

我有三个定义如下的表:

CREATE TABLE `roles` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `roles_title_index` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `role_user` (
  `role_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  KEY `role_user_user_id_foreign` (`user_id`),
  KEY `role_user_role_id_foreign` (`role_id`),
  CONSTRAINT `role_user_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE,
  CONSTRAINT `role_user_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我有两门课:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class User extends Model
{

    /**
     * Roles of the user
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany('App\Models\Role', 'role_user', 'user_id', 'role_id');
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
    /**
     * Returns users for role
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users()
    {
        return $this->belongsToMany('App\Models\User', 'role_user', 'role_id', 'user_id');
    }
}
现在,我如何从类用户方法中根据角色的标题筛选角色? 我想我可能需要以某种方式修改它:

$this->roles()->getResults();

所以,假设我需要一种方法来获取迁移表中用户类中的特定角色,您可以这样尝试

角色(迁移):-

 Schema::create('roles',function($table)
        {
            $table->integer('id')->unsigned()->nullable();
            $table->string('title',150);
            $table->string('remember_token',100);
            $table->timestamps();
        });
   Schema::create('users',function($table)
        {
        $table->increments('users_id')->index();
        $table->integer('roles_id')->unsigned()->nullable();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->string('email')->unique();
        $table->string('password',100);
        $table->string('remember_token',100);
        $table->timestamps();
        });
  Schema::create('role_user', function($table)
    {
        $table->increments('id')->unsigned();
        $table->integer('roles_id')->unsigned()->index();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users'); 
        $table->timestamps();
    });
class Roles extends Model  {

    protected $table = 'roles';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('users');
    }

}
class Users extends Model
{
    protected $table = 'users';
    public function roles(){
        return $this->hasMany('roles');
    }
}
用户(迁移文件):-

 Schema::create('roles',function($table)
        {
            $table->integer('id')->unsigned()->nullable();
            $table->string('title',150);
            $table->string('remember_token',100);
            $table->timestamps();
        });
   Schema::create('users',function($table)
        {
        $table->increments('users_id')->index();
        $table->integer('roles_id')->unsigned()->nullable();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->string('email')->unique();
        $table->string('password',100);
        $table->string('remember_token',100);
        $table->timestamps();
        });
  Schema::create('role_user', function($table)
    {
        $table->increments('id')->unsigned();
        $table->integer('roles_id')->unsigned()->index();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users'); 
        $table->timestamps();
    });
class Roles extends Model  {

    protected $table = 'roles';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('users');
    }

}
class Users extends Model
{
    protected $table = 'users';
    public function roles(){
        return $this->hasMany('roles');
    }
}
角色\u用户(迁移文件):-

 Schema::create('roles',function($table)
        {
            $table->integer('id')->unsigned()->nullable();
            $table->string('title',150);
            $table->string('remember_token',100);
            $table->timestamps();
        });
   Schema::create('users',function($table)
        {
        $table->increments('users_id')->index();
        $table->integer('roles_id')->unsigned()->nullable();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->string('email')->unique();
        $table->string('password',100);
        $table->string('remember_token',100);
        $table->timestamps();
        });
  Schema::create('role_user', function($table)
    {
        $table->increments('id')->unsigned();
        $table->integer('roles_id')->unsigned()->index();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users'); 
        $table->timestamps();
    });
class Roles extends Model  {

    protected $table = 'roles';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('users');
    }

}
class Users extends Model
{
    protected $table = 'users';
    public function roles(){
        return $this->hasMany('roles');
    }
}
运行迁移文件后,创建一个类似这样的模型类

模型类:

角色。模型:-

 Schema::create('roles',function($table)
        {
            $table->integer('id')->unsigned()->nullable();
            $table->string('title',150);
            $table->string('remember_token',100);
            $table->timestamps();
        });
   Schema::create('users',function($table)
        {
        $table->increments('users_id')->index();
        $table->integer('roles_id')->unsigned()->nullable();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->string('email')->unique();
        $table->string('password',100);
        $table->string('remember_token',100);
        $table->timestamps();
        });
  Schema::create('role_user', function($table)
    {
        $table->increments('id')->unsigned();
        $table->integer('roles_id')->unsigned()->index();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users'); 
        $table->timestamps();
    });
class Roles extends Model  {

    protected $table = 'roles';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('users');
    }

}
class Users extends Model
{
    protected $table = 'users';
    public function roles(){
        return $this->hasMany('roles');
    }
}
用户。型号:-

 Schema::create('roles',function($table)
        {
            $table->integer('id')->unsigned()->nullable();
            $table->string('title',150);
            $table->string('remember_token',100);
            $table->timestamps();
        });
   Schema::create('users',function($table)
        {
        $table->increments('users_id')->index();
        $table->integer('roles_id')->unsigned()->nullable();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->string('email')->unique();
        $table->string('password',100);
        $table->string('remember_token',100);
        $table->timestamps();
        });
  Schema::create('role_user', function($table)
    {
        $table->increments('id')->unsigned();
        $table->integer('roles_id')->unsigned()->index();
        $table->foreign('roles_id')->references('id')->on('roles'); 
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users'); 
        $table->timestamps();
    });
class Roles extends Model  {

    protected $table = 'roles';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('users');
    }

}
class Users extends Model
{
    protected $table = 'users';
    public function roles(){
        return $this->hasMany('roles');
    }
}
如果需要为透视表使用不同的名称,或重写关联的键,可以将它们作为可选参数传入

return $this->belongsToMany(
   'roles',          // related_table
   'user_conversations',     // pivot_table
   'users_id',                // key_1
   'roles_id'        // key_2
);
那么:

class User extends Model {
    // find all users having the given title
    public static function withTitle($title) {
        return static::whereHas('roles', function ($q) use ($title) {
            $q->where('title', $title);
        })->get();
    }

    // determine if this particular user has the given title
    public function hasRoleByTitle($title) {
        $roles = $this->roles()->where('title', $title)->get();
        return 0 < count($roles);
    }
}

未经测试,不在L5环境附近。但是应该在棒球场

您已经创建了三个表角色,Roles\u User,Users tables,对吗?@User这是问题中陈述的第一件事。告诉我role\u User是一个透视表?是的,我有三个表和两个类。角色\用户是数据透视表。我想从类用户中获取特定角色。我不知道如何根据用户id获取类用户中的特定角色。您可以获取特定角色。@Iamzozo感谢您的确认!删除了关于任何L4/L5差异的注释。实际上,我想检查用户是否有特定的role@user2975535啊,好的。更新。