Laravel Policy

iwanna
1 min readFeb 12, 2020

Policies are classes that organize authorization logic around a particular model or resource. For example, if your application is a blog, you may have a Post model and a corresponding PostPolicy to authorize user actions such as creating or updating posts.

Introduction

Laravel policy adalah bagian dari Authorization Laravel yang membantu Anda melindungi konten atau sumber daya dari akses unauthorized.

How to use

Via Views

@can('delete', $post)
// show post delete button
@endcan
@cannot('delete', $post)
// disable post delete button
@endcannot

Via Model

if ($user->can('delete', $post)) {
// add your actions here
}

Via Controller

public function delete(Post $post)
{
$this->authorize('delete', $post);
// The current user can delete the post
}

Lanjutan dari project :

Highlight

https://github.com/jeriatno/lr_policy

Result

--

--