【问题标题】:Laravel Return modified array into viewLaravel 将修改后的数组返回到视图中
【发布时间】:2018-03-24 11:59:58
【问题描述】:

假设我有这个 Laravel Eloquent 模型:

$scores = [];
$dataScores = Book::where('author_id', '=', $uid)
            ->select('score')
            ->get();

结果:

[{
    "score": 30
},
{
    "score": 100
},
{
    "score": 17
},
{
    "score": 75
},
{
    "score": 17
},
{
    "score": 0
},
{
    "score": 60
},
{
    "score": 100
},
{
    "score": 17
},
{
    "score": 67
},
{
    "score": 83
},
{
    "score": 50
},
{
    "score": 100
},
{
    "score": 50
},
{
    "score": 83
},
{
    "score": 38
},
{
    "score": 90
},
{
    "score": 10
},
{
    "score": 83
},
{
    "score": 83
},
{
    "score": 60
},
{
    "score": 80
},
{
    "score": 13
},
{
    "score": 33
},
{
    "score": 33
}]

所以,我想在返回view() 之前,我会这样做:

    for ($s=0; $s < count($dataScores); $s++) {
        array_push($scores, $dataScores[$s]->score);
    }

    return view(
        'layouts.dashboard.main',
        [
            'menu' => 'book-dashboard',
            'scores' => $scores
        ]
    );

所以,我会在 Laravel Blade 视图中访问 {{ $scores }},我想我会得到 ​​p>

$scores =  [
    30,
    100,
    17,
    75,
    17,
    0,
    60,
    100,
    17,
    67,
    83,
    50,
    100,
    50,
    83,
    38,
    90,
    10,
    83,
    83,
    60,
    80,
    13,
    33,
    33
];

但是我得到了:

htmlspecialchars() 期望参数 1 是字符串,给定数组。

但如果我只包含 score / x.. 的键/值的修改前数组,那么它返回正常。我只想要一个数组中的分数值。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我认为您可以使用pluck 收集方法来避免手动循环并获取仅包含您需要的键/值的映射数组

    <?php
    $scores = Book::where('author_id', '=', $uid)
       ->select('score')
       ->get()
       ->pluck('score');
    

    其实你说的

    但是如果我只包含 score / x.. 的键/值的修改前数组,那么它返回正常。我只想要一个数组中的分数值。

    因为 Eloquent Collections 对象已经实现了 __toString() 魔术方法来获取对象的字符串表示,所以如果你尝试这样做

    {{$scores}} //you will get [1,2,3,4,5] 
    //(the string representation defined into the __toString method)
    

    但是如果你尝试将它转换为一个数组,你会破坏函数签名,因为你试图传递一个数组而不是一个字符串

    因此,如果您需要将值转换为自定义字符串或逗号分隔值,您可以将 implode 方法附加到您的调用链中

    <?php
    $scores = Book::where('author_id', '=', $uid)
       ->select('score')
       ->get()
       ->pluck('score')
       ->implode(',');
    

    现在,如果您在视图中使用 {{$scores}},您将打印 '1,2,3,4,5,6'

    【讨论】:

    • 实际上pluck 方法本身运行一个foreach
    • 当然可以,但是如果你使用 laravel,你会尽量做到“雄辩”
    【解决方案2】:

    首先,你可以省略这段代码:

    for ($s=0; $s < count($dataScores); $s++) {
            array_push($scores, $dataScores[$s]->score);
        }
    

    相反,只需使用:

    $dataScores = Book::where('author_id', '=', $uid)
                ->select('score')
                ->get()
                ->pluck('score);
    

    这将产生一个类似 $scores = [1,2,3,4,5...] 的数组

    显然你不能用像 {{$scrores}} 这样的刀片来渲染它 你必须首先内爆数组。

    这样做:

    return view(
            'layouts.dashboard.main',
            [
                'menu' => 'book-dashboard',
                'scores' => implode(" ", $scores);
            ]
        );
    

    你会得到“1 2 3 4 5 6...”的输出

    【讨论】:

    • 其实他可以把那个变量渲染到blade中,因为集合对象实现了__toString方法
    • 哦,哇!我一定错过了文档中的pluck!谢谢你。我明白了。
    【解决方案3】:

    试试这样:

    $scores = [];
    $dataScores = Book::where('author_id', '=', $uid)
                ->select('score')
                ->get()
                ->toArray();
    
            foreach($dataScores as $key => $value){
                array_push($scores,$value);
            }    
    
            return view(
            'layouts.dashboard.main',
            [
                'menu' => 'book-dashboard',
                'scores' => $scores
            ]
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多