Mamulasa
Mamulasa

Reputation: 543

Laravel Check if Array is empty

I am struggling with displaying some content depending on if an array property does have a value or not. If an article has a title, I want to display the content of the entire article, if not I want to show something else. However, it doesn't work. The code in the else statement is not executed.

What's wrong here?

My Code is:

@foreach($restaurantmenue as $daily)
                    @foreach($daily->articles as $menue)
                     @if(!empty($menue->title))
                       <div class="card card-horizontal">
                         <div class="row">
                           <div class="col-md-5">
                             <div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
                               <img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
                               <div class="filter filter-azure">
                                 <button type="button" class="btn btn-neutral btn-round">
                                     <i class="fa fa-heart"></i> SMÄCKT MIR
                                 </button>
                               </div>
                             </div>
                           </div>
                           <div class="col-md-7">
                                <div class="content">
                                  <p class="category text-info">
                                       <i class="fa fa-trophy"></i> Best of
                                   </p>

                                   <a class="card-link" href="#">
                                       <h4 class="title">{{$menue->title}} </h4>
                                   </a>
                                   <a class="card-link" href="#">
                                       <p class="description">{{$menue->body}}</p>
                                       <br />
                                       {{$menue->price}} €
                                   </a>
                                    <div class="footer">
                                       <div class="stats">
                                           <a class="card-link" href="#">
                                              <i class="fa fa-male"></i> {{$restaurant->name}}
                                           </a>
                                       </div>
                                       <div class="stats">
                                         <a class="card-link" href="#">
                                           <i class="fa fa-comments"></i> 23 Comments
                                         </a>
                                       </div>
                                       <div class="stats">
                                          <a class="card-link" href="#">
                                           <i class="fa fa-heart"></i> 231 Likes
                                          </a>
                                       </div>
                                   </div>
                               </div>
                           </div>
                         </div>
                       </div>
                      @else
                        No Articles for Today
                     @endif
                    @endforeach
                @endforeach

Upvotes: 2

Views: 97161

Answers (3)

Mamulasa
Mamulasa

Reputation: 543

Well, the code is executed if the article is not empty. But The code in the else statement is still not executed. I still can't display the "no article" message it there are no articles available

  @if(!$restaurantmenue->isEmpty())
                    @foreach($restaurantmenue as $daily)
                        @foreach($daily->articles as $menue)

                           <div class="card card-horizontal">
                             <div class="row">
                               <div class="col-md-5">
                                 <div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
                                   <img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
                                   <div class="filter filter-azure">
                                     <button type="button" class="btn btn-neutral btn-round">
                                         <i class="fa fa-heart"></i> SMÄCKT MIR
                                     </button>
                                   </div>
                                 </div>
                               </div>
                               <div class="col-md-7">
                                    <div class="content">
                                      <p class="category text-info">
                                           <i class="fa fa-trophy"></i> Best of
                                       </p>

                                       <a class="card-link" href="#">
                                           <h4 class="title">{{$menue->title}} </h4>
                                       </a>
                                       <a class="card-link" href="#">
                                           <p class="description">{{$menue->body}}</p>
                                           <br />
                                           {{$menue->price}} €
                                       </a>
                                        <div class="footer">
                                           <div class="stats">
                                               <a class="card-link" href="#">
                                                  <i class="fa fa-male"></i> {{$restaurant->name}}
                                               </a>
                                           </div>
                                           <div class="stats">
                                             <a class="card-link" href="#">
                                               <i class="fa fa-comments"></i> 23 Comments
                                             </a>
                                           </div>
                                           <div class="stats">
                                              <a class="card-link" href="#">
                                               <i class="fa fa-heart"></i> 231 Likes
                                              </a>
                                           </div>
                                       </div>
                                   </div>
                               </div>
                             </div>
                           </div>



                        @endforeach
                    @endforeach
                  @else
                    no article
                @endif

Upvotes: 0

Kiran Subedi
Kiran Subedi

Reputation: 2284

If you are getting data using ORM in laravel like

$data = \App\Model::get();

then you can check whether the $data is empty or not as following :

@if(!$data->isEmpty())
     // $data is not empty
@else
    // $data is empty
@endif

Reference : Check Here

But if you use find or findOrFail, then you can check array as

$data = \App\Model::findOrFail($id);


@if(!empty($data))
     // $data is not empty
@else
    // $data is empty
@endif

Upvotes: 41

Terabyte
Terabyte

Reputation: 455

Hope this might help you.

$processes = [1, 2, 4, 5];
$maintenance = [1, 2, 3, 4, 5];

$array_value = function ($value, $array) {
    $str = "$value";
    if (in_array($str, $array)) {
        echo 5;
    } else {
        echo 1;
    }
};

foreach ($maintenance as $m) {
    $array_value ($m, $processes);
}

Basically I'm comparing two arrays and display 5 if data found on the other array

Upvotes: 0

Related Questions