2

I've upgraded our Laravel application from Laravel 5.2 to 5.3 and got a warning message from Laravel side

The Query Builder returns collections, instead of plain arrays, in Laravel 5.3. You will need to upgrade your code to use collections or chain the all() method onto your query to return a plain array.

Thats means fluent query builder now returns Illuminate\Support\Collection instances instead of plain arrays. This brings consistency to the result types returned by the fluent query builder and Eloquent.

You can standard this by below example

Laravel 5.2

    $users = DB::table('users')->select('id','first_name')->limit(10)->get();

   dd($users);

   Result : 
   ==========
    array:10 [▼
      0 => {#1423 ▼
        +"id": 12
        +"first_name": "John"
      }
      1 => {#1424 ▶}
      2 => {#1425 ▶}
      3 => {#1426 ▶}
      4 => {#1427 ▶}
      5 => {#1428 ▶}
      6 => {#1429 ▶}
      7 => {#1430 ▶}
      8 => {#1431 ▶}
      9 => {#1432 ▶}
    ]

Laravel 5.3

$users = DB::table('users')->select('id','first_name')->limit(10)->get();

dd($users);



 Result : 
   ==========
    Collection {#1428 ▼
      #items: array:10 [▼
        0 => {#1430 ▼
          +"id": 12
          +"first_name": "John"
        }
        1 => {#1431 ▶}
        2 => {#1432 ▶}
        3 => {#1433 ▶}
        4 => {#1434 ▶}
        5 => {#1435 ▶}
        6 => {#1436 ▶}
        7 => {#1437 ▶}
        8 => {#1438 ▶}
        9 => {#1439 ▶}
      ]
    }

When I am trying to access data by $users[0]->first_name getting same result thats is correct.

That creates confusion for me.. What is the actual difference here and what will be the impact on our application?

2 Answers 2

0

Laravel collections implement "Arrayable" which means they inherit all of the same functionality as normal arrays but also have the additional functionality that collections provide, so you will not get any issues accessing at index.

If you want to convert it back to a normal array, you could do ->toArray()

$users = DB::table('users')->select('id','first_name')->limit(10)->get()->toArray();
Sign up to request clarification or add additional context in comments.

Comments

0

I was worried about Array and Collection elements and lastly i know

That Collection class holds the collection elements in a protected property called $items (as you could see from your dump protected 'items' =>), which is of type array. The class also implements an interface called IteratorAggregate, which basically means it allows any variable of that type to be iterated using a foreach statement.

So in short Collection is an iterable object that can be treated as an array, but is better than an array because if offers extra methods that allow you to manipulate the items from the collection. You can check the Collection API to see a complete list of available methods.

Ref : In a Laravel 5 Collection how do you return an array of objects instead of an array of arrays?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.