foreach Laravel的问题

时间:2018-09-04 04:28:51

标签: php laravel foreach

此问题是My first question的延续

所以我有一个数组

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
                )

        )

)

我对该数组排序

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

我的观点

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

现在,我得到了

这样的数组
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
                )

        )

)

和我的foreach循环

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

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



                            </tr>
                            @endforeach

我希望这样输出

linto
shanu
shelin
joseph

但是我得到的输出是这样的

 joseph 
linto 
shanu 
shelin

任何帮助将不胜感激。预先感谢。

1 个答案:

答案 0 :(得分:0)

一定是您的变量在您未提及的代码中的某个地方被覆盖。

还请在执行雄辩的查询后对结果进行dd($ sorted),以查看是否根据需要从db中获取数据。