亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據用戶角色創建單獨的表

根據用戶角色創建單獨的表

PHP
慕少森 2022-11-04 16:31:41
我創建了一個名為“用戶”的表,在該表中用戶可以擁有三個角色之一。我正在嘗試創建三個單獨的表,以根據他們的角色將不同用戶分組到一個頁面或一個單獨的頁面中。目前,我將它們顯示在一張桌子上。用戶 index.blade.php@extends('layouts.app')@section('content')<div class="container">        <div class="row justify-content-center">                <div class="col-md-10">                    <p>                        <a href="{{ route('admin.users.create') }}"><button type="button" class="btn btn-success">Create User</button></a>                    </p>                    <div class="card">                            <div class="card-header">Manage Users</div>                            <div class="card-body">                            <div class="table-responsive">                                 <table class="table">                                    <thead>                                    <tr>                                        <th>ID</th>                                        <th>Name</th>                                        <th>Course</th>                                        <th>Role</th>                                         <th></th>                                        <th></th>                                        <th></th>                                    </tr>                                    </thead>                                    <tbody>我很感激任何關于我如何去做這件事的幫助。
查看完整描述

2 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

在控制器中,您需要按用戶分組role_name,然后在 index.blade.php 中使用 foreach 創建表?;蛘吣憧梢允褂?code>belongTorelashionship,按角色獲取用戶,如你所愿。$角色->用戶



查看完整回答
反對 回復 2022-11-04
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

您可以嘗試制作三個這樣的表


[數據庫]


users

    id - integer

    name - string


roles

    id - integer

    name - string


role_user

    user_id - integer

    role_id - integer

[模型]


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class User extends Model

{

    /**

     * The roles that belong to the user.

     */

    public function roles()

    {

        return $this->belongsToMany('App\Role');

    }

}

您可以在 [CONTROLLER]中附加許多角色


$user = App\User::find(1);

$user->roles()->attach($roleId);

``


查看完整回答
反對 回復 2022-11-04
  • 2 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號