jeudi 8 août 2019

Getting same result When accessing Plain Array and Collection Array in Laravel upgrade 5.2 to 5.3

I've upgrade our Laravel application from Laravel 5.2 to 5.3 and got a warring 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.

Thats create confusion for me.. What is actual difference here and what will be impact on our application.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire