从位于数组中的字符串获取收集结果

时间:2019-10-23 18:24:26

标签: laravel collections

为了避免表单中出现多行,我尝试使用raay中的键获取集合结果,但是它不起作用。这可能吗 ?怎么样?

<table>
    <tr>
        <td>
            <div class="thumbnailLeftSmallIconTop">
                <img class="thumbnailSmallImage" src="~/Content/images/house_50px.png" alt="Image">
                <span> @Html.DisplayFor(modelItem => house) </span>
            </div>
        </td>
        <td>
            <div class="thumbnailLeftSmallIconBottom">
                <img class="thumbnailSmallImage" src="~/Content/images/bedroom_80px.png" alt="Image">
                <span> @Html.DisplayFor(modelItem => bedroom) </span>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div class="thumbnailLeftSmallIconTop">
                <img class="thumbnailSmallImage" src="~/Content/images/house_50px.png" alt="Image">
                <span> @Html.DisplayFor(modelItem => house) </span>
            </div>
        </td>
        <td>
            <div class="thumbnailLeftSmallIconBottom">
                <img class="thumbnailSmallImage" src="~/Content/images/bedroom_80px.png" alt="Image">
                <span> @Html.DisplayFor(modelItem => bedroom) </span>
            </div>
        </td>
    </tr>
</table>

我得到500错误:stdClass类的对象无法转换为字符串...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果$collectionarrayCollection,例如:

$collection = [
    "lundiStatus" => "foo",
    "mardiStatus" => "foo"
];

$collection = new Collection([
    "lundiStatus" => "foo",
    "mardiStatus" => "foo"
]);

您可以通过以下方式访问:

@foreach($days as $day => $data)
   <?php dd($collection[$data->status]); ?>
@endforeach

但是如果$collectionobject,例如:

$collection = (object) array(
    "lundiStatus" => "foo",
    "mardiStatus" => "foo"
);

您可以通过以下方式访问:

@foreach($days as $day => $data)
   <?php dd($collection->{$data->status}); ?>
@endforeach

有关语句中对象的访问属性值,请参见括号