Do you generally pass in information from a controller into your livewire components?

I’m hoping to pick the brain of some people using the TALL stack on a regular basis. I’m just getting started and I am having trouble deciding with each component that I build whether I should make it completely independent (pulling in the information it needs on it’s own), or if I should pass in values via attributes that I got from a regular controller etc. When I see examples online, and when I look at already created livewire components such as the ones that come in the TALL ui preset, I often see both approaches.

To be more specific I want to clarify that I’m talking about normal data that I know is going to be on a page like an index from a resource etc. Should I be passing that collection into my livewire components or should I be just making my components fetch the data they need themselves?

I realize the answer will likely be to do both depending on the situation, but I’m looking for some guidance around this topic, and some rules of thumb etc.

Thanks!

I think there is no right answer to your question, that is why Livewire initialization made pretty flexible. In some situations you need to pass data from the controller (or parent component) in some situations you need to get parameters from query string and sometimes from Livewire events and components will fetch the rest. There is no “should” there. The only thing is important that component is able to have all needed data at the time of rendering.

My only recommendation is to make public properties of the component as less as possible, but this is more about security, then on the way how you link data.

1 Like

If it’s already going to be there, pass it in.

Take a user for example: If you are performing a query in your controller for that user, and then you do it again in your component, eloquent doesn’t catch that and the query will be made twice; so passing it in avoids that. You can use the debug bar to prove that. It’s a good tool for finding query duplication, among many other things, if you aren’t using it already.

1 Like