AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HandleInertiaRequests.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Billing\UsageLimits;
  4. use App\Enums\OrderStatus;
  5. use App\Models\Store;
  6. use App\Models\User;
  7. use Illuminate\Http\Request;
  8. use Inertia\Middleware;
  9. class HandleInertiaRequests extends Middleware
  10. {
  11. /**
  12. * The root template that's loaded on the first page visit.
  13. *
  14. * @see https://inertiajs.com/server-side-setup#root-template
  15. *
  16. * @var string
  17. */
  18. protected $rootView = 'app';
  19. /**
  20. * Determines the current asset version.
  21. *
  22. * @see https://inertiajs.com/asset-versioning
  23. */
  24. public function version(Request $request): ?string
  25. {
  26. return parent::version($request);
  27. }
  28. /**
  29. * Define the props that are shared by default.
  30. *
  31. * @see https://inertiajs.com/shared-data
  32. *
  33. * @return array<string, mixed>
  34. */
  35. public function share(Request $request): array
  36. {
  37. return [
  38. ...parent::share($request),
  39. 'name' => config('app.name'),
  40. 'auth' => [
  41. 'user' => $request->user(),
  42. ],
  43. 'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
  44. // The store tree and the plan box in the sidebar of every dashboard page.
  45. 'sidebar' => fn () => $request->user() !== null ? $this->sidebar($request->user()) : null,
  46. ];
  47. }
  48. /**
  49. * @return array{stores: list<array<string, mixed>>, usage: array<string, mixed>}
  50. */
  51. private function sidebar(User $user): array
  52. {
  53. return [
  54. 'stores' => $user->stores()
  55. ->orderBy('created_at')
  56. ->withCount(['orders as to_ship' => fn ($query) => $query->where('status', OrderStatus::Paid)->whereNull('shipped_at')])
  57. ->get()
  58. ->map(fn (Store $store) => [
  59. 'name' => $store->name,
  60. 'slug' => $store->slug,
  61. 'is_published' => $store->isPublished(),
  62. 'to_ship' => (int) $store->getAttribute('to_ship'),
  63. ])
  64. ->all(),
  65. 'usage' => app(UsageLimits::class)->for($user),
  66. ];
  67. }
  68. }