if I write a foreach loop like this, is the method all() called every loop again or only once?
foreach(User::all() as $user) { ... }
In C# I know, the all() function is only executed once. But in php also?
Or is it faster if I hold the data in a variable like this?
$users = User::all();
foreach($users as $user) { ...}
Both pieces of code will do the exactly same job and will create just one DB query, but I'd go with this for better readability:
$users = User::all();
foreach ($users as $user) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With