Laravel 如果 foreach 为空,则不显示类别
Laravel do not show category if foreach is empty
我有以下控制器和型号:
约会控制器:
if ($view === 'default') {
$appointments = Appointment::with('label')->with('status')->paginate(25);
}
if ($view === 'label') {
$appointments = Label::with('appointments')->paginate(25);
}
预约模型:
public function label()
{
return $this->belongsTo('App\Label');
}
标签型号:
public function appointments()
{
return $this->hasMany('App\Appointment');
}
在我的 "default" 视图中,显示以下内容:
并且在我的 "label" 视图中,显示以下内容:
我想要完成的是,当一个标签没有约会时,我根本不想显示这个标签。因此,在我的 "label view"(查看下图)中,我只希望显示 "business" 和 "personal" 标签(以及如图所示的约会),以及所有其他标签不应该显示。
在我看来,我只是在使用 foreach 循环。有没有正确方向的指示?
我的看法(简单):
@if ($view === 'label')
@foreach ($appointments as $appointment)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
@foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endforeach
</table>
@foreach ($appointments as $appointment)
@endif
写标签前需要检查数组的长度。所以在这里你要确保 $appointment->appointments count 大于 0.
@if ($view === 'label')
@foreach ($appointments as $appointment)
@if (count($appointment->appointments) > 0)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
@foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endforeach
</table>
@endif
@foreach ($appointments as $appointment)
@endif
试试这个:
您可以通过检查非空约会来实现此目的。在这里,我们将检查 empty () 方法的否定情况。
查看 (blade)
@if ($view === 'label')
@foreach ($appointments as $appointment)
<table>
@foreach ($appointment->appointments as $key=> $label)
@if(!empty($label))
@($key == 0)
<tr>
<th><p>{{ $appointment->label }}</p> </th>
</tr>
@endif
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endif
@endforeach
</table>
@endforeach
@endif
希望这对您有所帮助。
我有以下控制器和型号:
约会控制器:
if ($view === 'default') {
$appointments = Appointment::with('label')->with('status')->paginate(25);
}
if ($view === 'label') {
$appointments = Label::with('appointments')->paginate(25);
}
预约模型:
public function label()
{
return $this->belongsTo('App\Label');
}
标签型号:
public function appointments()
{
return $this->hasMany('App\Appointment');
}
在我的 "default" 视图中,显示以下内容:
并且在我的 "label" 视图中,显示以下内容:
我想要完成的是,当一个标签没有约会时,我根本不想显示这个标签。因此,在我的 "label view"(查看下图)中,我只希望显示 "business" 和 "personal" 标签(以及如图所示的约会),以及所有其他标签不应该显示。
在我看来,我只是在使用 foreach 循环。有没有正确方向的指示?
我的看法(简单):
@if ($view === 'label')
@foreach ($appointments as $appointment)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
@foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endforeach
</table>
@foreach ($appointments as $appointment)
@endif
写标签前需要检查数组的长度。所以在这里你要确保 $appointment->appointments count 大于 0.
@if ($view === 'label')
@foreach ($appointments as $appointment)
@if (count($appointment->appointments) > 0)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
@foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endforeach
</table>
@endif
@foreach ($appointments as $appointment)
@endif
试试这个:
您可以通过检查非空约会来实现此目的。在这里,我们将检查 empty () 方法的否定情况。
查看 (blade)
@if ($view === 'label')
@foreach ($appointments as $appointment)
<table>
@foreach ($appointment->appointments as $key=> $label)
@if(!empty($label))
@($key == 0)
<tr>
<th><p>{{ $appointment->label }}</p> </th>
</tr>
@endif
<tr>
<td>{{ $label->appointment }}</td>
</tr>
@endif
@endforeach
</table>
@endforeach
@endif
希望这对您有所帮助。