Php 如何在laravel查询中添加条件?(拉威尔5.3)
我在UserRepository.php中的函数如下:Php 如何在laravel查询中添加条件?(拉威尔5.3),php,laravel,repository,laravel-5.3,laravel-query-builder,Php,Laravel,Repository,Laravel 5.3,Laravel Query Builder,我在UserRepository.php中的函数如下: public function displayList($year, $subaccountcode = NULL) { $query = Self::orderBy('programcode') ->orderBy('accountcode') ->findWhere(['year' => $year]) if(isset
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
如何解决错误
更新
我正在使用第三方软件包。我从这里得到:这取决于
findWhere()
的功能。如果它类似于find()
,并返回一个对象,但不是查询生成器的实例,则执行以下操作:
$query = Self::orderBy('programcode')->orderBy('accountcode')->findWhere(['year' => $year]);
if (isset($subaccountcode)) {
$query->findWhere(['subaccountcode' => $subaccountcode]);
}
return $query;
$Subaccountcode
是否存在,->findWhere(['year'=>$year])
必须保持运行存在错误:1/1 Macroable.php第74行中的BadMethodCallException:方法findWhere不存在
@mosestoh此方法在Laravel中不存在,我以为它的方法是由您定义的。如果您确实定义了它,请说明您是如何定义的。如果没有,请使用where()
而不是findWhere()
和return$query->get()如果查询是这样的:public function displayList($year,$subaccountcode=NULL){$query=Self::orderBy('programcode')->orderBy('accountcode')->findWhere(['year'=>$year]);return$query;}
,它可以工作。但当我添加条件if时,它不是working@mosestoh当你使用第三方软件包时,你应该在问题中这样说。很抱歉,我对andersao/l5存储库不熟悉。我建议您编辑您的问题并添加此信息,也许其他人可以帮助您。