Laravel雄辩的不区分大小写数据

Laravel雄辩的不区分大小写数据,laravel,eloquent,Laravel,Eloquent,我在迁移中有一个名为currency\u codes的表,其中包含(id、source、code) 我还有一个模型名CurrencyCode.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class CurrencyCode extends Model { protected $table = 'currency_codes'; protected $fillable = ['sour

我在迁移中有一个名为
currency\u codes
的表,其中包含
(id、source、code)

我还有一个模型名
CurrencyCode.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CurrencyCode extends Model
{
    protected $table = 'currency_codes';
    protected $fillable = ['source', 'code'];
}

我想要的:每个操作
(验证、插入、搜索)
使用此
代码
字段将不敏感。我可以通过控制器控制这么多行代码。我可以通过雄辩的模型做到这一点吗?

当您使用mysql时,您的搜索查询和验证规则不区分大小写

e、 g:

如果希望代码始终以大写形式存储,可以使用

从文档中:

要定义mutator,请在模型上定义setFooAttribute方法 其中Foo是您希望访问的列的“studly”大小写名称。 同样,让我们为first_name属性定义一个mutator。这 当我们试图设置值时,将自动调用mutator 模型上的first_name属性的名称:

因此,在你的情况下:

/**
 * Set the currency code.
 *
 * @param  string  $value
 * @return void
 */
public function setCodeAttribute($value)
{
    $this->attributes['code'] = strtoupper($value);
}

我不明白你的问题,你能详细说明你想做什么吗?我想查询
code
不区分大小写。比如:我将编写
where('code','=',$inputCode)
,它将进行不敏感的搜索。有没有办法操纵雄辩的模型?你试过你的问题吗?如果您使用的是mysql,它应该不区分大小写。如果我想检查
code
字段上的唯一性验证是什么?只需使用
unique
规则测试它,它对我有效,不区分大小写。
$query->where('code', $code)
'code' => 'unique:currency_codes,code'
class User extends Model
{
    /**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
     */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }
}
/**
 * Set the currency code.
 *
 * @param  string  $value
 * @return void
 */
public function setCodeAttribute($value)
{
    $this->attributes['code'] = strtoupper($value);
}