【发布时间】:2020-04-02 05:23:55
【问题描述】:
我希望你能理解这个问题,事实上我在我的模型中创建了两种方法,一种用于按类别过滤记录,另一种用于按城市。
但是当我转到我的控制器时,在公共函数索引中,我认为它不能同时调用两者,希望在示例中更好地理解。
public function **scopeBuscadorCategoria** (Builder $query) {
if (session()->has('s')){
$query->where('nombre', 'LIKE', "%". session('s') ."%");
}
if (session()->has('dcategoria')){
$query->where('categoria_id', session('dcategoria'));
session()->remove('dcategoria');
}
return $query->paginate();
}
public function **scopeBuscadorLocalidad** (Builder $query) {
if (session()->has('dlocalidad')){
$query->where('localidad_id', session('dlocalidad'));
session()->remove('dlocalidad');
}
return $query->paginate(21);
}
如您所见,这是我在模型中创建的两种方法。
public function index (){
**$destinos = Destino::buscadorcategoria();**
$categorias = Categoria::paginate(6);
$localidades = Localidad::all();
return view('destinos.index', compact('destinos', 'categorias', 'localidades'));
}
我认为这是问题所在,在控制器中,“$destinos = Destino::buscadorcategoria();”,我只能放 1 个方法,在这种情况下它会过滤类别,但是如果我需要按城市过滤,我必须删除类别的方法并放入城市,例如“$destinos = Destino::buscadorlocalidad();”
你能在变量 $destinos 中调用两个方法吗?在这种情况下你能做什么?我需要按类别和城市进行过滤。
希望大家能帮帮我,非常感谢。
【问题讨论】:
-
您不应该在模型范围函数中使用
->paginate()或->get()
标签: php laravel function methods controller