I am trying to remove the last two row in a Laravel Collection. The size of the Collection can vary, but I will always want to remove the last two. I managed it by doing this, but out of curiosity, do you think there is a better way to do this ?
Here is my way :
$results = $results->reverse()->slice(2)->reverse();
Thanks a lot
John
The Collection object's slice() method works similarly to array_slice()
, allowing a negative value for the length argument, so you should be able to keep it simple and just do
$results = $results->slice(0, -2);
You can use the pop method twice.
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