【问题标题】:2nd where being case sensitive in Laravel Eloquent在 Laravel Eloquent 中区分大小写的第二个地方
【发布时间】:2021-08-25 04:07:14
【问题描述】:

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

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

我面临的问题是,当我使用$this->employees 获取第一条记录时,它是否区分大小写。例如Employee NumberEMP1

这会返回员工。

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

这将返回 null。

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

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

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    你可以使用 LIKE %...%

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

    【讨论】:

    • 还是空吗?
    • 是的,当我尝试输入“LIKE”时,它仍然区分大小写并且 % 不起作用。
    【解决方案2】:

    在本例中使用 Upper/Lower 函数。

    $this->employees->where('employee_number', 'LIKE', '%'.strtoupper($value).'%')->first();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-19
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多