Speeding Up Laravel Tests with tmpfs (MySQL in RAM)
If your Laravel test suite is painfully slow and does a lot of database work (multi-tenancy, migrations, seeding), the bottleneck is almost…
When using Inertia.js in a Laravel application, page-specific data returned from your controller will override shared data with the same key. This can lead to unexpected behavior.
Let's say you have a notification system in your Laravel + Inertia.js application. You share notification data across all pages using Inertia's middleware:
// app/Http/Middleware/HandleInertiaRequests.php
public function share(Request $request): array
{
return [
'notifications' => [
'items' => $this->getNotifications(),
'unread_count' => $this->getUnreadCount(),
'alerts' => $this->getAlerts()
]
];
}
This shared data is used in your layout component to display a notification bell with a dropdown:
<!-- AppLayout.vue -->
<NotificationBell
:notifications="$page.props.notifications.items"
:unread-count="$page.props.notifications.unread_count"
/>
Now, you create a notifications page to display all notifications:
// NotificationController.php
public function index()
{
return Inertia::render('Notifications/Index', [
'notifications' => Notification::latest()->get() // This overwrites shared data!
]);
}
You might expect this to work fine since your shared data is always available, but you'll encounter errors because:
notifications data completely replaces the shared notifications dataitems, unread_count, etc.)There are two main approaches to fix this:
// NotificationController.php
public function index()
{
return Inertia::render('Notifications/Index', [
'allNotifications' => Notification::latest()->get() // Different key
]);
}
<!-- Notifications/Index.vue -->
<script setup>
defineProps({
allNotifications: {
type: Array,
required: true
}
});
</script>
Change your Controller to use the same structure as in your shared data. This will still override the shared data but as the structure is the same there probably won't be any errors.
// NotificationController.php
public function index()
{
return Inertia::render('Notifications/Index', [
'notifications' => [
'items' => Notification::latest()->get(),
'unread_count' => Notification::unread()->count(),
'alerts' => []
]
]);
}
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com