1

Here I have a foreach loop of teachers with their associated relationships which are reviews. As I loop through the teacher if they have no relationship or review I want to print "No Reviews" in the containing div. The code i use now works, however if one teacher has a review the other classes remain empty instead of saying No Review. Is there a way in laravel or in js i can do this? It's probably a simple solution for this. Can someone help me out?

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>");
}

/--------------second attempt ---------/

if (isEmpty($('.reviewblock'))) {
        $('.reviewblock').append("<div class = 'empty'><h1>No Reviews</h1></div>");
    }

2 Answers 2

4

Your code will do it only for the first occurrence of the class ".reviewBlock". Do it for all the occurrences.

$('.reviewblock').each(function(i, obj) {
    if (isEmpty(this))) {
        $('.reviewblock').append("<div class = 'empty'><h1>No Reviews</h1></div>");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

He just asked laravel or js. I just gave my opinion on JS since you've done it in laravel.
2

All you have to do is chekc if the variable is empty

<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>

NOTE: Ah I forgot, remove all of the JS that you use to append HTML, you don't need that. Also would advise you not to use <H1> tag to style you page, this should be used only once on each page and that normally goes to the title/header of the page, otherwise Google might penalise you.

1 Comment

Still not printing anything? Maybe switch the if and the else statements?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.