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…
In Laravel applications with Inertia.js and Vue, sharing data between components often involves provide/inject patterns, or custom composables.
Inertia's page props offer a simple built-in alternative. They require no additional setup.
Inertia page props provide a straightforward mechanism for data sharing. Here's what you need to know:
Let's walk through the implementation process, starting from the server and moving to the frontend components.
First, we need to set up our initial props on the server. This is typically done in the HandleInertiaRequests middleware:
class HandleInertiaRequests extends Middleware
{
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'credits' => $request->user()->credits,
]);
}
}
This code adds a 'credits' property to our Inertia page props, making it available to all components in our Vue application.
Now, let's look at a component that consumes these props. This component will display the user's credits:
<script setup>
import { computed } from 'vue';
import { usePage } from '@inertiajs/vue3';
const page = usePage();
const credits = computed(() => page.props.credits);
</script>
<template>
<div>Credits: {{ credits }}</div>
</template>
This component uses Inertia's usePage composable to access the page props. We create a computed property credits that will automatically update whenever the page.props.credits value changes.
Next, let's examine a component that updates the credits value:
<script setup>
import { usePage } from '@inertiajs/vue3';
import axios from 'axios';
const page = usePage();
const updateCredits = async () => {
const response = await axios.post('/api/use-credits');
page.props.credits = response.data.credits;
};
</script>
This component defines an updateCredits function that makes an API call and then updates the credits value in the Inertia page props. The beauty of this approach is that any other component using this prop (like Component A) will automatically update to reflect the new value.
Using Inertia page props for inter-component communication offers several benefits:
While this approach is powerful, it's important to keep a few things in mind:
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