簡體   English   中英

在laravel查詢中如何添加條件? (laravel 5.3)

[英]How to add condition if in laravel query? (laravel 5.3)

我在UserRepository.php中的功能是這樣的:

public function displayList($year, $subaccountcode = NULL)
{
    $query = Self::orderBy('programcode')
                 ->orderBy('accountcode')
                 ->findWhere(['year' => $year])
            if(isset($subaccountcode))
                 ->findWhere(['subaccountcode' => $subaccountcode]);
            else
                ;
    return $query;
}

我添加了一個條件來檢查子帳戶代碼是否存在。 我試圖喜歡它,但是存在錯誤:

 2/2 ReflectionException in Container.php line 809: Class App\Repositories\UserRepository does not exist 

我該如何解決錯誤?

UPDATE

我使用第三方程序包。 我從這里得到: https : //github.com/andersao/l5-repository

這取決於findWhere()功能。 如果它類似於find()並且返回一個對象,但不是查詢生成器的實例,則執行以下操作:

$query = Self::orderBy('programcode')->orderBy('accountcode')->findWhere(['year' => $year]);

if (isset($subaccountcode)) {
    $query->findWhere(['subaccountcode' => $subaccountcode]);
}

return $query;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM