2016-09-06 170 views
-2

我很努力地顯示一些內容,取決於數組屬性是否有值。 如果一篇文章有​​一個標題,我想顯示整篇文章的內容,如果不是,我想顯示其他內容。但是,它不起作用。 else語句中的代碼未執行。Laravel檢查數組是否爲空

這裏有什麼問題?

我的代碼是:

@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 
+0

放的print_r($ menue)通過更換線@if文章(!空($ menue->標題)),並檢查內容。 –

+0

這是輸出:App \ Article Object([fillable:protected] => Array([0] => title [1] => body [2] => price [3] => facebook_page_id [4] ... – Mamulasa

+0

可能有些東西,用isset' –

回答

-1

希望這可以幫助你。

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

基本上我比較兩個陣列和顯示器5如果另一個陣列上發現的數據

2

如果您獲得在laravel使用ORM數據等

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

那麼,可以檢查是否$data爲空或不如下:

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

參考Check Here

但是如果你使用findfindOrFail,那麼你可以檢查數組作爲

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


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

我正在使用//顯示當前配置文件的日常菜餚 $ restaurantmenue = User :: where('id',$ id) - > with(['articles'=> function($ q){ $ q-> nowpublished(); }]) - > get(); – Mamulasa

+0

您可以使用'$ restaurantmenue-> isEmpty()'來檢查數組是否爲空 – sixFingersMan

+0

else語句上的代碼仍未執行。 – Mamulasa

-1

那麼,是如果文章不爲空,執行的代碼。但是else語句中的代碼仍然沒有執行。我仍然無法顯示「沒有了」的消息就沒有可用的

@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