【问题标题】:Laravel collection, how to reference like $item->name instead of $item['name']Laravel 集合,如何引用 $item->name 而不是 $item['name']
【发布时间】:2018-01-18 08:59:33
【问题描述】:

我也想知道他们俩叫什么名字?例如一个像 $item['name'] 和 $item->name 引用的数组项,如果它们有一个术语的话! (不是键,值)

$items = collection([
    [
        'name' => 'test item 1',
        'description' => 'this is a description',
    ],
    [
        'name' => 'test item 1',
        'description' => 'this is a description',
    ],
    [
        'name' => 'test item 1',
        'description' => 'this is a description',
    ],
]);

然后在刀片或任何我喜欢能够引用它们的地方

foreach($items as $item) {
    echo $item->name;
}

而不是

foreach($items as $item) {
    echo $item['name'];
}

编辑

已解决,谢谢各位大神解答。

$collection->map(function ($section) {
    return (object) [
        'label' => $section['label'],
        'items' => collect($section['items'])->map(function ($item) {
            return (object) $item;
        }),
    ];
});

下面的完整代码也用于检查集合中是否存在项目。

protected $collection = [];

public function __construct()
{
    parent::__construct();
    $this->collection = collect([
        [
            'label' => 'Section label 1',
            'items' => [
                [
                    'label' => 'Item label 1',
                    'description' => 'Item description',
                ],
            ],
        ],
        [
            'label' => 'Section label 2',
            'items' => [
                [
                    'label' => 'Item label 2',
                    'description' => 'Item description',
                ],
            ],
        ],
        [
            'label' => 'Section label 3',
            'items' => [
                [
                    'label' => 'Item label 3',
                    'description' => 'Item description',
                ],
            ],
        ],
    ])
    ->map(function ($section) {
        return (object) [
            'label' => $section['label'],
            'items' => collect($section['items'])->map(function ($item) {
                return (object) $item;
            }),
        ];
    });
}

public function show($slug = '')
{
    $item = $this->getItem($slug);

    if (null === $item) {
        abort(404);
    }

    return view('show')
        ->withItem($item);
}

protected function getItem($slug)
{
    return $this
        ->collection
        ->flatMap(function ($section) {
            return $section->items;
        })
        ->first(function ($item) use ($slug) {
            return str_slug($item->label) === $slug;
        });
}

【问题讨论】:

  • 所以你需要创建 object 而不是 array
  • Regolith 打败了我。要完成您的问题,请在此处命名 $item->name 是对象的属性。另外,集合作为函数不存在,它只需要collect()
  • collect(['array items go here']new Illuminate\Support\Collection(['array items']) 不是collection()

标签: php arrays laravel collections


【解决方案1】:

试试这个

$items= json_decode(json_encode((object) $items), FALSE);

json_decode第二个参数如果为真则将结果转换为array否则结果为object

查看here 以供参考

【讨论】:

    【解决方案2】:

    您可以使用Collection::map 函数。

    $items = $items->map(function($item) { return (object) $item; })
    

    然后在你的模板中:

    @foreach ($items as $item)
        {{ $item->name }}
    @endforeach
    

    【讨论】:

    • 谢谢你,它帮助我计算出我的对象,我将更新我使用的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多