Ezra
Ezra

Reputation: 247

Issue with foreach Laravel

This Question is continuation of My first question

So I have an array

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 79
                    [name] => shelin
                    [status] => 0
                )

            [1] => stdClass Object
                (
                    [id] => 80
                    [name] => shanu
                    [status] => 2
                )

            [2] => stdClass Object
                (
                    [id] => 81
                    [name] => linto
                    [status] => 2
                )

            [3] => stdClass Object
                (
                    [id] => 82
                    [name] => joseph
                    [status] => 0
                )

        )

)

I sort this array

$sorted = $collection->sortByDesc('status');

my view

return view('voyager::users.viewusersAppraisals')->with('values', $sorted);

Now, I got array like

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
                [2] => stdClass Object
                    (
                        [id] => 81
                        [name] => linto
                        [status] => 2
                    )

            [1] => stdClass Object
                (
                    [id] => 80
                    [name] => shanu
                    [status] => 2
                )

            [0] => stdClass Object
                (
                    [id] => 79
                    [name] => shelin
                    [status] => 0
                )

            [3] => stdClass Object
                (
                    [id] => 82
                    [name] => joseph
                    [status] => 0
                )

        )

)

and my foreach loop

 @foreach($values as $data)<?php

?>
                            <tr>
                                <td>{{$data->name}}</td>



                            </tr>
                            @endforeach

I expect output like so

linto
shanu
shelin
joseph

But I get output like so

 joseph 
linto 
shanu 
shelin

Any help would be appreciated. Thanks in advance.

Upvotes: 1

Views: 88

Answers (1)

Varun.Kumar
Varun.Kumar

Reputation: 192

It must be your variable be getting overwritten somewhere in the code which you have not mentioned.

Also please dd($sorted) your result after executing the eloquent query to see whether you are getting data from db in right format as per your need.

Upvotes: 1

Related Questions