-
-
Save monbang/3eafac6efd0be5d4759d2f2ad8cdb339 to your computer and use it in GitHub Desktop.
Gate auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| File: AuthServiceProvider | |
| public function boot() | |
| { | |
| $this->registerPolicies(); | |
| Gate::define('update-product', function(User $user, Product $product) { | |
| return $product->user->is($user); | |
| }); | |
| } | |
| File: Product.php | |
| public function user() { | |
| return $this->belongsTo(User::class); | |
| } | |
| File: products.blade.php | |
| @can('update-product', $product) | |
| <button class="update-product">Edit</button> | |
| @endcan | |
| File: ProductController.php | |
| public function store(Product $product) { | |
| $this->authorize('update-product', $product); | |
| $product->save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment