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

Laravel

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
    {
        //
    }
}

Follow me!

コメント

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