【Laravel】artisanのmake:policyコマンドを解説

チートシート

Laravel 10.xのartisanの「make:policy」コマンドを解説します。

Description

新しいポリシークラスを作成します。

Usage

php artisan make:policy [-f|--force] [-m|--model [MODEL]] [-g|--guard [GUARD]] [--] <name>

Arguments

引数必須設営
name引数ポリシークラス名

Options

オプション省略形必須説明
forcef-ポリシーがすでに存在する場合でもクラスを作成
modelm-ポリシーが適用されるモデル
guardg-ポリシーが依拠するガード

Examples

no options

php artisan make:policy FooPolicy
   INFO  Policy [app/Policies/FooPolicy.php] created successfully.  
<?php

namespace App\Policies;

use App\Models\User;

class FooPolicy
{
    /**
     * Create a new policy instance.
     */
    public function __construct()
    {
        //
    }
}

model

php artisan make:policy --model=User FooPolicy
   INFO  Policy [app/Policies/FooPolicy.php] created successfully.  
<?php

namespace App\Policies;

use App\Models\User;
use Illuminate\Auth\Access\Response;

class FooPolicy
{
    /**
     * Determine whether the user can view any models.
     */
    public function viewAny(User $user): bool
    {
        //
    }

    /**
     * Determine whether the user can view the model.
     */
    public function view(User $user, User $model): bool
    {
        //
    }

    /**
     * Determine whether the user can create models.
     */
    public function create(User $user): bool
    {
        //
    }

    /**
     * Determine whether the user can update the model.
     */
    public function update(User $user, User $model): bool
    {
        //
    }

    /**
     * Determine whether the user can delete the model.
     */
    public function delete(User $user, User $model): bool
    {
        //
    }

    /**
     * Determine whether the user can restore the model.
     */
    public function restore(User $user, User $model): bool
    {
        //
    }

    /**
     * Determine whether the user can permanently delete the model.
     */
    public function forceDelete(User $user, User $model): bool
    {
        //
    }
}

Stub

OptionStub file name
-policy.stub
modelpolicy.plain.stub

Source

10.x

framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php at 10.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

11.x

framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

コメント

タイトルとURLをコピーしました