从DATABASE恢复用户角色(外键) [英] Recuperating roles of users from DATABASE (Foreign key)

查看:63
本文介绍了从DATABASE恢复用户角色(外键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何从数据库中恢复用户角色? 知道我有3个表(用户,角色,User_role) 迁移:

Can anyone tell me how i can recuperate roles of users from my database? Knowing i have 3 tables (Users, Roles, User_role) Migrations:

表用户:

 public function up()
      {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

        });
    }

表角色:

public function up()
       {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description');
            $table->timestamps();
        });
       }

表user_role:

Table user_role:

public function up()
         {
        Schema::create('user_role', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('role_id');
            $table->timestamps();
        });
       }

我的控制器中返回用户列表的函数是membrevis():

the function in my controller that returns the list of users is membrevis():

 public function membrevis()
     {
      $filter = isset($_GET['filter']) ? $_GET['filter'] : null;
        $query = DB::table('users')
        ->join('user_role', 'users.id', '=', 'user_role.user_id')
        ->join('roles', 'user_role.role_id', '=', 'roles.id')
        ->where('users.valid','=',1)
        ->select('users.*','roles.description');

    if ($filter != null) {
        $query->where('users.name','like','%'.$filter.'%')
            ->orWhere('roles.description','like','%'.$filter.'%');
         }

       $itemsPerPage = 8 ;
       $currentPage  = isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ? $_GET['page'] : 1;
         $urlPattern   = '/membre2?page=(:num)';
         $totalItems   = $query->count();
      $donner   = $query->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();
       $paginator = new  Paginator( $totalItems, $itemsPerPage, $currentPage, $urlPattern );

      return view('membre2',['users'=> $donner,'paginator'=> $paginator]);
     }

我在这里可以进行哪些修改以恢复用户及其角色?外键问题

what can i modify here to recuperate users and roles of them? foreign keys problem

推荐答案

我建议您使用Laravel模型和关系.

I recommend you using Laravel Models and relationships.

在这里,用户和角色之间存在关系.这种关系是在称为 intermediate 表(在本例中为 user_roles 表)中定义的.

Here you have a relationship between Users and Roles. This relationship is defined in what is called an intermediate table, in this case the user_roles table.

用户和角色之间存在多对多类型的关系,其中一个用户可以拥有多个角色,而一个角色类型可以分配给许多用户.

There is a Many-to-Many type of relationship here between users and roles, where one user can have many roles and one role type can be assigned to many users.

我建议您阅读以下内容: https://laravel.com /docs/5.2/eloquent-relationships#many-to-many

I recommend you reading this: https://laravel.com/docs/5.2/eloquent-relationships#many-to-many

这是针对Laravel 5.2的,但是您会看到针对每个Laravel 5.x版本的文档.在那里,您将学习如何建立这些关系以及如何有效地使用它们.

That's for Laravel 5.2, but you'll see that documentation for every Laravel 5.x version. There you'll learn how to build these relationships and how to use them effectively.

这篇关于从DATABASE恢复用户角色(外键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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