首页 > 解决方案 > 在 Laravel Eloquent 中区分大小写的第二个地方

问题描述

我的 Laravel Excel 导入中有一个构造函数来节省资源。

public function __construct()
{
    $this->employees = EmployeeInformation::where('company_id', Auth::user()
                                          ->company_id)
                                          ->select('id', 'employee_number')->get();
}

我面临的问题是当我$this->employees用来获取 1 记录时它变得区分大小写。例如Employee NumberEMP1

这将返回员工。

$this->employees->where('employee_number', 'EMP1')->first();

这将返回 null。

$this->employees->where('employee_number', 'emp1')->first();

有什么办法可以节省资源吗?我想要实现的是使用$this->employees来搜索员工的记录,以避免在我的导入中查询每行。

标签: phpmysqllaraveleloquent

解决方案


您可以使用 LIKE %...%

$this->employees->where('employee_number', 'LIKE', '%emp1%')->first();

推荐阅读