我有个拉威尔项目。它有一个表,我想用表单中的数据填充该表。目前,它用表中可能存在的任何数据填充表单。
我使用@foreach遍历数据对象。当表中有数据时,这种方法工作得很好,但是当没有数据时,@foreach数据对象将为空。人们如何处理这一点,使表单仍然以默认值显示?
@foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
</div>
<div class="col-12">
<div class="row">
<div class="col-md-2">
<label for="hirePrice" class="">Hire Price</label>
</div>
<div class="col-md-4">
<input type="text" name="hire" class="form-control" placeholder="1200" aria-label="hireprice" id="hirePrice" value="{{ $price->hire}}">目前,如果我清空表格,页面只显示标题,而不显示上面的html,包括表单域。
编辑:表格将始终只有1行!!不确定这是否会让它变得更容易。
发布于 2021-01-07 03:00:55
You can use
@isset($pricing)
@foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
.................................
//your code
@endforeach
@endisset
Or, You can use if else condition like this:
@if($pricing != Null)
@foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
.....................
//code
@endforeach
@else
<div>No data</div>
@endif发布于 2021-01-09 01:53:06
虽然我不能了解你的实际情况,但我可以给你一些建议。您可以像这样在input标记内添加一行条件: HTML:variable }}”@else value=“”@endif >
否则,您可以将foreach循环中的相同代码放入else条件中。因此,当表中存在数据时,它以表单的形式显示数据,否则将出现空值的相同表单。
https://stackoverflow.com/questions/65599939
复制相似问题