zoukankan      html  css  js  c++  java
  • laravel中model字段的类型定义

    在HasAttributes中的两段方法:

    public function setAttribute($key, $value)
        {
            // First we will check for the presence of a mutator for the set operation
            // which simply lets the developers tweak the attribute as it is set on
            // the model, such as "json_encoding" an listing of data for storage.
            if ($this->hasSetMutator($key)) {
                $method = 'set'.Str::studly($key).'Attribute';
    
                return $this->{$method}($value);
            }
    
            // If an attribute is listed as a "date", we'll convert it from a DateTime
            // instance into a form proper for storage on the database tables using
            // the connection grammar's date format. We will auto set the values.
            elseif ($value && $this->isDateAttribute($key)) {
                $value = $this->fromDateTime($value);
            }
    
            if ($this->isJsonCastable($key) && ! is_null($value)) {
                $value = $this->castAttributeAsJson($key, $value);
            }
    
            ………………
        }
    
    
    protected function isJsonCastable($key)
        {
            return $this->hasCast($key, ['array', 'json', 'object', 'collection']);
        }
    
    protected function castAttributeAsJson($key, $value)
        {
            $value = $this->asJson($value);
    
            if ($value === false) {
                throw JsonEncodingException::forAttribute(
                    $this, $key, json_last_error_msg()
                );
            }
    
            return $value;
        }
    

      

    protected function castAttribute($key, $value)
        {
            if (is_null($value)) {
                return $value;
            }
    
            switch ($this->getCastType($key)) {
                case 'int':
                case 'integer':
                    return (int) $value;
                case 'real':
                case 'float':
                case 'double':
                    return (float) $value;
                case 'string':
                    return (string) $value;
                case 'bool':
                case 'boolean':
                    return (bool) $value;
                case 'object':
                    return $this->fromJson($value, true);
                case 'array':
                case 'json':
                    return $this->fromJson($value);
                case 'collection':
                    return new BaseCollection($this->fromJson($value));
                case 'date':
                    return $this->asDate($value);
                case 'datetime':
                    return $this->asDateTime($value);
                case 'timestamp':
                    return $this->asTimestamp($value);
                default:
                    return $value;
            }
        }
    

      所以,只需要在model中定义$casts数组,例如:

    protected $casts = [
            'personal_access_client' => 'bool',
            'password_client' => 'bool',
            'revoked' => 'bool',
        ];
    

      存:

      laravel会通过在通过casts中定义的类型对接收的数据进行验证,然后通过第一段方法,转为相应的类型存到数据库。

      取:

      通过第二段方法将数据库中查出的数据转为casts数组中定义的类型,在返回出来。

      强大!

  • 相关阅读:
    Microsoft 365 开发篇:建立医疗组织的Microsoft Teams 模板
    Microsoft 365 开发篇:将SharePoint Online的Site Collection保存为模板
    Microsoft 365 开发:updateRecordingStatus API 发布
    Microsoft 365开发篇:Microsoft Build 2020 关于Graph API 更新
    线上公开认知课:软件开发
    Microsoft Build 2020:Microsoft Teams 新功能概览
    Microsoft 365 开发篇:Teams Export API支持数据导出
    201306114357—实验2
    test
    正则表达式
  • 原文地址:https://www.cnblogs.com/buerr/p/7122339.html
Copyright © 2011-2022 走看看