我在$users = User::fromCountry('DE')
->isMaster()
->paginate(20);
类中有两个返回不同查询的方法。我想像这样使用
isMaster()
但它不起作用,因为User
是来自$users = User::fromCountry('DE')
->(User::isMaster())
->paginate(20);
的方法,而不是来自查询构建器的方法:
调用未定义的方法Illuminate \ Database \ Query \ Builder :: isMaster()
我也试过
User
但这也行不通。是否可以轻松组合User
类的查询?
以防必要,以下是public static function isMaster()
{
switch (orga) {
case 1:
return static::where('modeIN','=','MT');
case 2:
return static::where('modeICI','=','CMT');
case 3:
return static::where('modeWHO','=','HMT');
}
}
public static function fromCountry($country)
{
return static::where(
function($query) use($country){
$query->whereHas('addresses',
function($q) use($country){
$q->where('idCountry','=',$country);
}
)
->orWhereHas('institutes',
function($q) use($country){
$q->whereHas('addresses',
function($q) use($country){
$q->where('idCountry','=',$country);
});
}
);
});
}
类中的两种方法:
Loop
答案 0 :(得分:1)
尝试将模态功能更改为:
public function scopeIsMaster($query)
{
switch (orga) {
case 1:
return $query->where('modeIN','=','MT');
case 2:
return $query->where('modeICI','=','CMT');
case 3:
return $query->where('modeWHO','=','HMT');
}
}
public function scopeFromCountry($query,$country)
{
return $query->where(
function($query) use($country){
$query->whereHas('addresses',
function($q) use($country){
$q->where('idCountry','=',$country);
}
)
->orWhereHas('institutes',
function($q) use($country){
$q->whereHas('addresses',
function($q) use($country){
$q->where('idCountry','=',$country);
});
}
);
});
}
答案 1 :(得分:1)
这是你可以做的。 Laravel使用范围来做这样的事情
User {
public function scopeOfType($query, $orga)
{
switch ($orga) {
case 1:
return $query->where('modeIN','=','MT');
case 2:
return $query->where('modeICI','=','CMT');
case 3:
return $query->where('modeWHO','=','HMT');
}
}
}
然后这样称呼它。
User::ofType(1);
User::ofType(2);
User::ofType(3);
答案 2 :(得分:0)
查看Laravel Scope:)