2

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
6
  • put a print_r($menue) by replacing the line @if(!empty($menue->title)) and check the content. Commented Sep 6, 2016 at 7:10
  • This is the output: App\Article Object ( [fillable:protected] => Array ( [0] => title [1] => body [2] => price [3] => facebook_page_id [4] ... Commented Sep 6, 2016 at 7:15
  • there might be something. try with isset Commented Sep 6, 2016 at 7:30
  • with isset there is the same relust as with !empty. Can somebody explain why the obove is not working? Is it really that difficult ? Commented Sep 6, 2016 at 7:43
  • We don't know what are you storing in $menue, instead of print_r($menue) try print_r($menue->title) and tell us the content. Commented Sep 6, 2016 at 7:44

3 Answers 3

41

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
Sign up to request clarification or add additional context in comments.

5 Comments

I am using //Show Daily Dishes for current Profile $restaurantmenue = User::where('id', $id)->with(['articles' => function ($q){ $q->nowpublished(); }])->get();
You can use $restaurantmenue->isEmpty() to check whether array is empty or not
The code on the else statement is still not executed.
It's only executed if the data is empty. The logic is correct, maybe there's something else we are not seeing in your code?
Fantastic answer
0

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

Comments

0

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

Comments

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.