1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelでblade内での認可

Posted at

#はじめに
Gateでアクセス制限を定義したのち、blade内で権限によってコンテンツの表示非表示を決めていきます。

###Gateでの定義

AuthServiceProvider.phpにおいて

    public function boot()
    {
        $this->registerPolicies();

        Gate::define('teacher',function ($user){
            return($user->role == 1);
        });

        Gate::define('student',function ($user){
            return($user->role == 2);
        });
    }

このように定義しておきます。

###bladeでの認可

  • @canディレクティブ
@can('teacher')
<p>you are teacher!</p>
@elsecan('student')
<p>you are not teacher!!</p>
@endcan
  • @cananyディレクティブ

認可対象が複数の場合は @cananyディレクティブを使うと纏まります。

@canany(['teacher','student'])
<p>you are teacher or student!</p>
@endcanany

これらのディレクティブは他にもGateで定義した際の引数を持たせたりできます。
以下をご参考に。
認可 6.x Laravel

以上!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?