Laravel 5.8 - in_array给我错误:'htmlspecialchars()

我在控制器中有一个这样的功能:


public function convert($id) {

        $project        = ProjectMaster::findOrFail($id);

        $items          = ProjectItem::all()->where('id_project_master', $id);

        $deliveryOrder  = ProjectDeliveryOrder::where('id_project', $id)->first();

        $itemsDO        = ProjectItemDeliveryOrder::all()->where('id_deliveryorder', $deliveryOrder->id)->pluck('id_item')->toArray();


        return view('delivery-order-form-add', compact('project', 'value', 'items', 'itemsDO'));

    }

$items给我结果:


{

"5": {

"id": 6,

"id_project_master": 6,

"name": "Item 1",

"qty": 2,

"cost": "1,000,000",

"totalcost": "2,000,000",

"rate": "2,000,000",

"totalrate": "4,000,000",

"created_at": "2020-01-24 03:23:25",

"updated_at": "2020-01-24 03:23:25"

},

   "6": {

       "id": 7,

       "id_project_master": 6,

       "name": "Item 2",

       "qty": 2,

       "cost": "2,500,000",

       "totalcost": "5,000,000",

       "rate": "4,000,000",

       "totalrate": "8,000,000",

       "created_at": "2020-01-24 03:23:25",

       "updated_at": "2020-01-24 03:23:25"

    }

}

并给我结果:$itemsDO


[

6

]

然后我有一个循环,在每个循环中,验证是否存在于刀片视图中,例如:in_array($this, $array)


@foreach ($items as $item)

<tr>

    <td class="text-right"><input type="checkbox" class="form-check-input" name="id_item[]" value="{{ $item->id }}" @if(in_array($item->id, $itemsDO)) disabled @endif></td>

</tr>

@endforeach

此验证给我一个错误 。我写错了参数或没有在刀片Laravel上工作?htmlspecialchars() expects parameter 1 to be string, array givenin_array


函数式编程
浏览 87回答 3
3回答

人到中年有点甜

Blade 期望并处于单独的行上。您可以改用三元 IF:@if@endif@foreach ($items as $item)<tr>&nbsp; &nbsp; <td class="text-right"><input type="checkbox" class="form-check-input" name="id_item[]" value="{{ $item->id }}" {{ (in_array($item->id, $itemsDO)) ?&nbsp; "disabled" : "" }}></td></tr>@endforeach

胡子哥哥

您的错误在语句中。if您应该说如果条件为真,请禁用。在你写的方式是一个,但你应该把它介绍为.echodisablesimple stringhtml code所以简而言之,你应该调用函数disableecho

江户川乱折腾

用这个代码解决:<td&nbsp;class="text-right"><input&nbsp;type="checkbox"&nbsp;class="form-check-input"&nbsp;name="id_item[]"&nbsp;value="{{&nbsp;$item->id&nbsp;}}"&nbsp;@if(in_array($items[$i]->id,&nbsp;$itemsDO))&nbsp;disabled&nbsp;@endif></td>$i是循环增量
打开App,查看更多内容
随时随地看视频慕课网APP