Created
July 28, 2022 23:04
-
-
Save Biegalski-LLC/181c4b2fd3cef48decffeef25c19b17a to your computer and use it in GitHub Desktop.
Add Permission & Roles In Laravel Jetstream Inertia
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
<?php | |
namespace App\Http\Middleware; | |
use Illuminate\Http\Request; | |
use Inertia\Middleware; | |
use Tightenco\Ziggy\Ziggy; | |
class HandleInertiaRequests extends Middleware | |
{ | |
/** | |
* The root template that is loaded on the first page visit. | |
* | |
* @var string | |
*/ | |
protected $rootView = 'app'; | |
/** | |
* Determine the current asset version. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return string|null | |
*/ | |
public function version(Request $request) | |
{ | |
return parent::version($request); | |
} | |
/** | |
* Define the props that are shared by default. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
public function share(Request $request) | |
{ | |
return array_merge(parent::share($request), [ | |
'ziggy' => function () use ($request) { | |
return array_merge((new Ziggy)->toArray(), [ | |
'location' => $request->url(), | |
]); | |
}, | |
'user.permissions' => function () use ($request) { | |
return ( $request->user() ? $request->user()->getAllPermissions()->pluck('name') : null ); | |
}, | |
'user.roles' => function () use ($request) { | |
return ( $request->user() ? $request->user()->roles()->pluck('name') : null ); | |
}, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment