SoFunction
Updated on 2025-03-10

laravel field formatting model field type conversion method

Some fields are encrypted and stored with base64_decode, and they have to be queried and decoded in each query. More troublesome, laravel's model provides method to handle it

In the corresponding model

The format is (set/get)(field name, note 1)(Attribute)

Note:

1. The first letter is capitalized, and the letters are capitalized in the underlined place. For example, the way to write product_json here is ProductJson

/**
  * The name field is processed when querying the user
  *
  * @author Eric
  * @param $value
  * @return string
  */
public function getNameAttribute($value)
{
  return base64_decode($value);
}
 
/**
  * The name field is processed when adding users
  *
  * @author Eric
  * @param $value
  * @return string
  */
public function setNameAttribute($value)
{
  return base64_encode($value);
}

The above article on the laravel field formatting model field type conversion method is all the content I share with you. I hope you can give you a reference and I hope you can support me more.