Skip to content

Instantly share code, notes, and snippets.

@Biegalski-LLC
Created July 28, 2022 23:04
Show Gist options
  • Save Biegalski-LLC/181c4b2fd3cef48decffeef25c19b17a to your computer and use it in GitHub Desktop.
Save Biegalski-LLC/181c4b2fd3cef48decffeef25c19b17a to your computer and use it in GitHub Desktop.
Add Permission & Roles In Laravel Jetstream Inertia
<?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