mardi 28 novembre 2017

Laravel How to skip elements if query is null in foreach loops?

I want to show latest posts in each category. If all category is not null, its OK, But if there are empty categories Trying to get property of non-object error. (I mean if category dosen't have any post)

So how can I pass thos categories post, when query returns null ?

Controller;

$categories=Category::with('posts')->latest()->get();
return view('frontend.home',compact('categories');

Blade;

@foreach($categories as $category)

<div class="col-md-3">
  <div class="card text-white">
    <a href="#"> <img class="card-img"
       src=""  alt="Card image">                                             

      <div class="card-img-overlay">
      <h4 class="card-title"></h4>
      </div>
    </a>
  </div>
</div>

@endforeach

Any advice ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hYSyos
via IFTTT

Aucun commentaire:

Enregistrer un commentaire