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

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

如何檢查用戶角色并在 laravel 中顯示選擇選項

如何檢查用戶角色并在 laravel 中顯示選擇選項

PHP
慕仙森 2023-04-02 15:12:16
我有不同角色的用戶,如管理員、員工、秘書等。我有一個發送信件的頁面,在這個頁面中我有一個select option顯示指標。我希望當具有秘書角色的用戶打開此頁面時,看到所有指標,但具有其他角色的其他用戶只能看到一個指標,如內部信件,我該怎么做?role我和之間有關系user:用戶模型public function roles(){    return $this->belongsToMany(Role::class);}好榜樣public function users(){    return $this->belongsToMany(User::class);}這是select option在發送信件頁面:<select class="col-12 border mt-2 pt-2" name="indicator_id">        @foreach($indicators as $indicator)                <option value="{{ $indicator->id }}">{{ $indicator->name }}</option>        @endforeach</select>如您所見,指標來自其他地方。這是顯示發送信件頁面的信件控制器:$indicators = Indicator::all();return view('Letter.add', compact('indicators'));
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

將此功能添加到檢查用戶角色的用戶模型中:


   /**

 * Check if this user belongs to a role

 *

 * @return bool

 */

 public function hasRole($role_name)

 {

     foreach ($this->roles as $role){


         //I assumed the column which holds the role name is called role_name

         if ($role->role_name == $role_name)

             return true;

      }

     return false;

 }

現在在您看來,您可以這樣稱呼它:


<select class="col-12 border mt-2 pt-2" name="indicator_id">

    @foreach($indicators as $indicator)    

          @if (Auth::user()->hasRole('Secretary'))

                <option value="{{ $indicator->id }}">{{ $indicator->name }}</option>

          @elseif (!Auth::user()->hasRole('Secretary') && {{ $indicator->name }} == 'internalLetter')

               <option value="{{ $indicator->id }}">Internal Letter</option>

          @endif

    @endforeach   


</select>


查看完整回答
反對 回復 2023-04-02
  • 1 回答
  • 0 關注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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