【发布时间】:2019-07-20 22:13:13
【问题描述】:
我在我的 laravel 项目中使用 EloquentFilter。
我有一张表,其中包含 First_name、Mid_name、Las_name、Type、Status 列。
这些是我的过滤器:
public function name($name){
return $this->orWhere('first_name', 'LIKE', "%$name%")
->orWhere('mid_name', 'LIKE', "%$name%")
->orWhere('las_name', 'LIKE', "%$name%");
}
public function isAvailable(){
return $this->where('status', 1);
}
public function thisType(){
return $this->where('type', 2);
}
public function setup(){
$this->thisType();
$this->isAvailable();
}
这是模型:
<?php
namespace App;
use App\ModelFilters\AdminFilters\UserFilter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Filterable;
protected $table = 'users';
protected $fillable = [
'id',
'first_name',
'mid_name',
'las_name',
'type',
'status'
];
# Other
public function modelFilter()
{
return $this->provideFilter(UserFilter::class);
}
}
所以如果我使用User:filter(Input::all())->get();,将自动返回2个结果,因为类型列的固定值(2)。
但我的问题是当我搜索“a”时,会返回 5 个结果。为什么在type列中并非全部有2个值时返回5个结果?
当它返回 5 个结果时,我怀疑它的功能是 ->orWhere。谁能帮我解决这个问题?
【问题讨论】:
-
你能发布模型的完整代码以及你在哪里使用它吗?这使您更容易理解您要做什么
-
执行
$user->name()之类的代码在哪里? -
@DmitriChebotarev 实际上我在 ajax 中使用它,但出于测试目的,我输入 URL,例如当我
localhost:8000/users/show它显示 2 个数据时。但是当我localhost:8000/users/show?name=a它显示 6 个数据。 -
@DmitriChebotarev 如果您的意思是在我使用的控制器内部,则仅在我的控制器内部有一行
return User::filter(Input::all())->get(); -
您在屏幕截图中的所有记录的名字、姓氏和中间名中似乎都有一个
a。所以这似乎是正确的。
标签: laravel eloquent laravel-5.8