In this episode, we dive into a super practical performance tip for your Laravel Livewire components: don't pass entire model objects as props if you don't need to.
We start by setting up a big list of fake employees, generating a database with thousands of records to really stress test the issue. We then create Livewire components to display these employees, and demonstrate how passing whole models to sub-components (like each row of a table) can have a huge impact on your app's memory usage and request duration.
Then we show you the difference: instead of passing the whole object, just pass the bits of data you actually need (like an employee's ID and name). It instantly halves memory usage and speeds things up dramatically, even with massive datasets. We check the results in Laravel Debugbar to see the real improvements.
We also address some common scenarios, like what to do if you need more employee data later (like for actions such as delete or edit). Instead of passing the full model, you just use the employee ID to re-fetch the model only when needed (using computed properties). We caution about performance pitfalls here as well, especially if you try accessing the model inside a loop—this can lead to lots of unnecessary queries!
Throughout the episode, we give you alternatives for commonly-needed data (like accessor methods), showing how to pass them down as separate props without the overhead. TL;DR: Only pass what you need, and your Livewire app will say thanks with much better performance!