duanna3634 2018-06-20 00:18
浏览 27
已采纳

从DATABASE(外键)恢复用户的角色

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

Table users:

 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();

        });
    }

Table roles:

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

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

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

  • 写回答

1条回答 默认 最新

  • dongsi073898 2018-06-20 00:35
    关注

    I recommend you using Laravel Models and relationships.

    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.

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题