分类
PLAN A

laravel 模型访问器和修改器

https://learnku.com/docs/laravel/7.x/eloquent-mutators/7502

要访问的字段需使用「驼峰式」命名

比如访问 city 那么命名为

getCityAttribute

app\Models\ClueInfo.php

    public function getCityAttribute()
    {
        $idCard= $this->customer_id;
        if(strlen($idCard)!=18) {
            return '';
        }
        $registeredResidence_province = substr($idCard, 0, 2);
        $province= DB::table('china_area')->where('code2',$registeredResidence_province)->value('name');  //获取省
        $registeredResidence_city = substr($idCard, 0, 4);  //4位到市级
        $registeredResidence_city= $registeredResidence_city.'00000000';
        $city= DB::table('china_area')->where('code',$registeredResidence_city)->value('name');   //这三行获取市级
        return $province.$city;
    }

访问这个字段值时,会返回给定值,但是数据库内值不会自信改变