Laravel Collections or Foreach? Shorter? More Readable?

preview_player
Показать описание
I want to raise a discussion. Laravel Collections allow you to avoid writing loops, also chaining the methods to make the code shorter. But is it really better or more readable? Let's discuss this.

Resources mentioned:

- - - - -
Рекомендации по теме
Комментарии
Автор

Young devs: new and short = better
Experienced devs: readable = better

borissman
Автор

We were using the first method (foreach) when we have jr devs to easily understand the code.
Then we switched to collection method and started commenting on each line to explain what that line or what that function means. It also help our jrs to understand and follow that pattern on their development also.

ajithlal
Автор

Thanks for your opening discussion, for me I will use both to apply affective and efficient on my application. We just need to understand the difference use cases of them. For me, I'm ready for both.

tony-go-code
Автор

The "magic" collection functions chained means running multipule loops over the same data. So, if there are n items in the collection and if we're doing 3 things (a filtering, a map and pluck for instance), a foreach can be written that executes the comparison to find the end of the loop n times, the chained solution will will always perform that loop the number of functions we chain times, or in this case 3n times. In formal Big-O, T(n) for both methods is on order of n, but in the real world those extra cycles are being used. So one other consideration to the chaining is as the collection get larger, the less you'll want to chain and the more you'll want to write a longer map or go back to foreach than use the convenience functions.

JeremyStreich
Автор

i do use collection and collection chaining and it is one liner but sometime when I do some logic inside map() then filter() or reduce() or whatever it still looks very long to look at,
it still neat and cool for short logic things and of course it is better to use in both conditions instead of for each and nested loops

stormcorexz
Автор

Is there any performance difference? I think foreach loop is more memory efficient than collection method.

kocha
Автор

For junior or newer developer like me, I agree with you. I think to use Laravel Collection you must have Intermediate skills. But if my skills like you I

ryanhidayathatuala
Автор

What a great video. Yes collections is amazing if you build an app that will get a lot of customers. For each is ok when using on apps where you are not so worries about optimization as it will take more power to perform than a collection. In reality we should use MongoDb or something similar where we can just have that server perform most of this in one iteration.

someguytu
Автор

Agree. I had to avoid using Collection methods because most of my team members are either not aware of them or not interested in learning Collections in depth. I do use them on my personal projects though.

waqasmushtaq
Автор

To be honest I love the old way more, but after 20 years programming vanilla php I am trying to adapt to Laravel. Because of all the "magic", collections as an example it is hard to adapt.

The older code is easier to understand when you come from an other language. If you are new to it, you have to do a lot of research and even then it is not always clear why and how it works.

JohnnyBigodes
Автор

I have used both of these approaches. For me it just depends wether my mind knows if there is another sexy way to do it or if not then do it in a traditional way. I think it's not just about it's more sexy, maybe it's even more readble and even it's quicker for development.

peymanGhaderkurehpaz
Автор

What is the best practice for collections used in blade

prasannadhanapala
Автор

It's true for the team that has many different skill levels. I had to admit it that at the first time I get started Laravel it confused me when I saw advanced Eloquent chaining.

christoherright
Автор

I prefer foreach () over ->each. But functions like ->pluck() and ->map() I wil always prefer using Collections over classic PHP. Filter is a little bit in the middle, it really depends on the amount of logic that goes into the filter. The Higher Order Functions I tend to stay away from, it looks really nice however it is a little bit confusing, one time the -> refers to a function and the other time -> refers to an property of the object.

Wiejeben
Автор

Hi big fan here. Can you kindly make an architecture series of video on how you and your team built both the front end and back end of quick admin panel so we junior devs can fully understand what's entails to build a complex website. Thanks

kwesimensah
Автор

Personally, I do collections best way especially for the API there is a no more efficient way to work with data, apart from Abstracting with Services, Data Transfer Object(DTO), apart from that I think if you work with the framework you must learn it if not go RTFM as usually :).

GamePlayByFaks
Автор

To be honest the old way is understandable only for people who got used to writing like this. The new sexy Laravel way is better because it's more compared to natural language. And I don't see a problem where the junior does not understand this (will not understand other concepts of Laravel as well). Junior has to know where the collection documentation is, Laravel has perfect documentation and there are not as many collection methods. If there is a problem remembering them, just make yourself a cheatsheet, most of them are pretty self-explaining.

colorscream
Автор

... and also the first one is more performant and elastic (open world) than the second one.

biliyonnet
Автор

In my opinion both are readable enough. So shorter is better. Collections.
But I understood, that I never used transformation of array to collection for using its methods. Could be a point.

MarkLeidman
Автор

Can you shot a video about Mockery and advanced unit testing?

mohamedhaj