Livewireでアクションを認可するには、任意のコンポーネントでAuthorizesRequests
トレイトを使用してから、コントローラー内で通常行うように$this->authorize()
を呼び出します。一例をご覧ください。
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class EditPost extends \Livewire\Component
{
use AuthorizesRequests;
public $post;
public function mount(Post $post)
{
$this->post = $post;
}
public function save()
{
$this->authorize('update', $this->post);
$this->post->update(['title' => $this->title]);
}
}
別のガードを使用してユーザーを認証する場合は、livewire設定ファイルのmiddleware_groupにもエントリを追加します。
...
'middleware_group' => ['web', 'auth:otherguard'],
...