search

Home  >  Q&A  >  body text

php - Problems encountered when using the reader (getAttribute) in laravel

The scene is:

When displaying the tel field in a Users table in the foreground, the last 4 digits of the phone number are replaced with *.

The method I use is:

class User extends Model
{
    public function getTelAttribute () {
        return substr($this->attributes['tel'], 0, 7) . '****';
    }
}

If you write it like this, it will generally be fine.

However, when the user modifies personal information through the form, the phone number displayed in the input is

Example: 1391234**

What I want to solve is:

In the case of using the reader (getAttribute), how to
temporarily cancel the action to the reader (getAttribute) under certain conditions, so as to display the complete phone number?

PHP中文网PHP中文网2788 days ago748

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 13:00:48

    1. Make two

    ExportArray时隐藏一个,这样做有弊病,导出的JSON接口中,telare all**

    protected $hidden = [
        'tel_original'
    ];
    
    function getTelAttribute() {
        return substr($this->attributes['tel'], 0, 7) . '****');
    }
    
    function getTelOriginalAttribute() {
        return $this->attributes['tel'];
    }

    2. Runtime replacement

    No changetel,但是在模板中显示时,用函数替换一下
    这样对导出JSONhas no effect, it just needs to be replaced with a function every time it is displayed

    reply
    0
  • Cancelreply