在这里,我有一个教师循环,他们的相关关系是评论。如果他们没有任何关系或评论,我会通过老师,我想打印" No Reviews"在包含div中。我现在使用的代码有效,但是如果一位老师有一个评论,其他课程仍然是空的,而不是说不评论。在laravel或js中有没有办法可以做到这一点?它可能是一个简单的解决方案。有人可以帮助我吗?
HTML:
@foreach($teachers as $teacher)
<div class="item{{{ $isFirst ? ' active' : '' }}}">
<div class = "deck">
<div class = "row marg">
<div class="col-md-8">
<div class = "stdnt">
<h1>Teacher Info</h1>
{!! Form::model( $student, ['url' => ['tchUpd', $teacher->id], 'method' => 'post', 'role' => 'form'] ) !!}
<h1>{{ucwords($teacher->name)}}</h1>
<div class = "form-group fga">
{!!Form::label('title', 'NickName:', ['class' => 'control-label lad']) !!}
{!!Form::text('nick', $teacher->nick, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Email:', ['class' => 'control-label lad']) !!}
{!!Form::text('email', $teacher->email, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Position:', ['class' => 'control-label lad']) !!}
{!!Form::text('pack', $teacher->position, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Bio-Short:', ['class' => 'control-label lad']) !!}
{!!Form::text('short', $teacher->short, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Prof/Edu:', ['class' => 'control-label lad']) !!}
{!!Form::text('profedu', $teacher->profedu, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Interests:', ['class' => 'control-label lad']) !!}
{!!Form::text('interest', $teacher->interest, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::label('title', 'Pitch:', ['class' => 'control-label lad']) !!}
{!!Form::text('why', $teacher->why, ['class'=> 'input-mini ina'])!!}
</div>
<div class = "form-group fga">
{!!Form::submit('Submit', ['class'=> 'btn btn-danger form-control'])!!}
</div>
{!! Form::close() !!}
</div>
</div>
<div class = "col-md-4">
<div class = "teac">
<h1>Teacher Info</h1>
<div id = "user-profile" class = "addyphoto" style = "background-image: url(/assets/image/{{$teacher->photo}})"> </div>
{!! Form::model( $student, ['url' => ['tchUpd', $teacher->id], 'method' => 'post', 'role' => 'form', 'novalidate' => 'novalidate', 'files' => true] ) !!}
<div class = "form-group fga">
{!!Form::label('title', 'Picture:', ['class' => 'control-label lad']) !!}
{!!Form::text('photo', $teacher->photo, ['class'=> 'input-mini ina tch'])!!}
</div>
<div class = "form-group fga">
{!! form::file('newphoto', null) !!}
{!!Form::submit('Submit', ['class'=> 'btn btn-danger form-control'])!!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
<div class = "mask flow">
<div class = "row-fluid marg reviewblock">
@foreach($teacher->reviews as $review)
<div class = "ablock">
<h1>{{ucwords($review->title)}}</h1>
<p>{{$review->created_at}}</p>
<p>{{$review->title}}</p>
<p>{!!$review->description!!}</p>
</div>
@endforeach
</div>
</div>
{{--*/ $isFirst = false; /*--}}
</div>
JS
if ( $('.reviewblock').children().length == 0 ) {
$(this).append("<div class = 'empty'><h1>No Reviews</h1></div>");
}
/ --------------第二次尝试--------- /
if (isEmpty($('.reviewblock'))) {
$('.reviewblock').append("<div class = 'empty'><h1>No Reviews</h1></div>");
}
答案 0 :(得分:4)
您的代码只会在第一次出现“.reviewBlock”类时执行此操作。对所有事件都这样做。
$('.reviewblock').each(function(i, obj) {
if (isEmpty(this))) {
$('.reviewblock').append("<div class = 'empty'><h1>No Reviews</h1></div>");
}
});
答案 1 :(得分:2)
如果变量为空,你所要做的就是chekc
<div class = "row-fluid marg reviewblock">
@if (empty($teacher->reviews))
<div class = 'empty'><h1>No Reviews</h1></div>
@else
@foreach($teacher->reviews as $review)
<div class = "ablock">
<h1>{{ucwords($review->title)}}</h1>
<p>{{$review->created_at}}</p>
<p>{{$review->title}}</p>
<p>{!!$review->description!!}</p>
</div>
@endforeach
@endif
</div>
注意:啊,我忘了,删除你用来附加HTML的所有JS,你不需要它。还建议您不要使用<H1>
标记来设置您的页面样式,这应该只在每个页面上使用一次,通常会转到页面的标题/标题,否则Google可能会对您施加惩罚。