mstdmstd's avatar

Can I work in view templates with array of data ?

I review my laravel/blade/livewire code for better performance and I try to remove any objects from blade files, like

$item->related_subitems->count

or

foreach( $item->related_subitems() as ... )

also I think it is better to pass to view template array of data, not object or collections. But trying to implemented it, I encountered that to use pagination of listing I need to pass collenction of models.

  1. Are there some pagination implementation with array?
  2. What do you this about using objects/arrays in blade templates? If there is a sence to move to array of data?

Please share your expierence if any.

Thanks!

0 likes
4 replies
vincent15000's avatar

I'm not sure that there are really performance problems with collections. With Laravel, a collection is an array (of objects).

Sure you can manage the pagination without any collection / object, but then you have to put in your array the needed informations (first page, number of pages, current page, ...).

You can also map your collection to an array, then retrieving exactly what you want / need. But this is then a one more action before displaying the view.

1 like
rolandocruzv's avatar

I agree with @vincent15000 , there is none performance problems with collections, what you will want to care about is what those collections data have, so don't send data to the frontend that you won't use, so, sometimes instead of sending a collections of models, with all their data inside, maybe you want to think about it and use DTOs that will have only the data you will need on the frontend, so be careful not sending duplicated data to the frontend

2 likes
Snapey's avatar

Why have you formed this opinion? It is without substance.

How much time do you waste converting objects to plain arrays?

1 like

Please or to participate in this conversation.