HandleInertiaRequests.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Illuminate\Http\Request;
  4. use Inertia\Middleware;
  5. class HandleInertiaRequests extends Middleware
  6. {
  7. /**
  8. * The root template that's loaded on the first page visit.
  9. *
  10. * @see https://inertiajs.com/server-side-setup#root-template
  11. *
  12. * @var string
  13. */
  14. protected $rootView = 'app';
  15. /**
  16. * Determines the current asset version.
  17. *
  18. * @see https://inertiajs.com/asset-versioning
  19. */
  20. public function version(Request $request): ?string
  21. {
  22. return parent::version($request);
  23. }
  24. /**
  25. * Define the props that are shared by default.
  26. *
  27. * @see https://inertiajs.com/shared-data
  28. *
  29. * @return array<string, mixed>
  30. */
  31. public function share(Request $request): array
  32. {
  33. $user = $request->user() ?? null;
  34. $user?->load('chats');
  35. return [
  36. ...parent::share($request),
  37. 'app' => [
  38. 'title' => config('app.name'),
  39. ],
  40. 'flush' => [
  41. 'success' => session('success'),
  42. 'json' => session('json')
  43. ],
  44. 'auth' => [
  45. 'user' => $user,
  46. 'chats' => fn() => $user?->chats->toResourceCollection(),
  47. ]
  48. ];
  49. }
  50. }