R
R
Ruslan Absalyamov2018-03-25 09:49:50
Laravel
Ruslan Absalyamov, 2018-03-25 09:49:50

Why is a collection needed?

Do I need to wrap the array when outputting to the front of the collection? And in general, why do we need a collection if working with an array is already using approximately the same methods as with a collection?
I just read the documentation and did not understand why to use it if you can just work directly with an array

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Ernest Faizullin, 2018-03-25
@erniesto77

since version 5.3 you can just return what you have. Any format in api routes is converted to json
if you have Laravel 5.3 or higher, you don’t have to worry

D
Dmitry Chekanov, 2018-03-25
@LanDer931

Collections are more convenient, IMHO.
For example, it is more convenient for me to use the each method of the collection than foreach.
Also with sorting, filtering, all actions can be laid out readably.

$collection
    ->filter()
    ->sort()
    ->each();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question