2016-12-21 89 views
0

我最近在laravel編碼,但我遇到了一些我不太瞭解的東西,我怎麼能用laravel爲以下代碼運行multiple where子句查詢?Laravel多與3 if語句在哪裏?

@if ($businesses->where('business_owner_id', '=', Auth::user()->id)->where('business_status', '=', 'accepted')->count() > 0) 
         You own a business... 
@elseif ($businesses->where('business_owner_id', '=', Auth::user()->id)->where('business_status', '=', 'pending')->count() > 0) 
         You have a business, but it is currently pending. 

    @else 
     <p>You can apply to create your own business by clicking the button below.</p> 
     <a href="/business/create"><div class="btn btn-default">Create a business</div></a> 
    @endif 

錯誤:解析錯誤:語法錯誤,意外 ')'

如何通過$企業?

Cache::remember('overview.businesses', 1, function() { 
      return Businesses::get(); 
     }); 

     return view('frontend.business.overview')->withBusinesses(Cache::get('overview.businesses')); 
+0

這裏有什麼錯誤? –

+0

我已更新我的問題。 –

+0

你在哪一行得到錯誤? –

回答

1

你最好不要做那些種在你的意見過濾器/搜索/計算的,他們應該是不聰明,所以我將其更改爲

$businesses = Cache::remember('overview.businesses', 1, function() { 
    return Businesses::get(); 
}); 

$acceptedCount = $businesses->where('business_owner_id', '=', Auth::user()->id)->where('business_status', '=', 'accepted')->count(); 

$pendingCount = $businesses->where('business_owner_id', '=', Auth::user()->id)->where('business_status', '=', 'pending')->count(); 

return view('frontend.business.overview', compact('acceptedCount', 'pendingCount')); 

然後你只需要做在你看來:

@if ($acceptedCount > 0) 
    You own a business... 
@elseif ($pendingCount > 0) 
    You have a business, but it is currently pending. 

@else 
    <p>You can apply to create your own business by clicking the button below.</p> 
    <a href="/business/create"><div class="btn btn-default">Create a business</div></a> 
@endif 
+0

謝謝你的回答,它工作很好,除了它表示$ acceptedCount在視圖中是未定義的? –

+0

請檢查變量名稱。我只是編輯來簡化傳遞給視圖的變量。 –