laravel中model类中好用的方法

    public function field()
    {
        return $this->belongsTo(HrmAuthFieldsModel::class, 'filed_id', 'id');
    }

    public function children()
    {
        return $this->hasMany(HrmAuthFuncFieldsModel::class, 'father_id');
    }

    public function getIsEmptyAttribute()
    {
        if (count($this->children) > 0) {
            return false;
        }

        return true;
    }

    public function scopeValid($query)
    {
        $query->where('hrm_auth_func_fields.status', self::STATUS_VALID);
    }

public function getReadableSizeAttribute()
{
if ($this->is_dir == self::IS_DIR_YES) {
return '--';
}

return StringUtil::formatFileSize($this->file_size);
}

public function getLastModifiedAttribute()
{
$time = strtotime($this->updated_at);
return date('Y-m-d H:i', $time);
}
 

1. 根据属性获取方法

2. getIsEmptyAttirbute() => model->is_empty

3. scopeValid() => model->valid()

原文地址:https://www.cnblogs.com/cjjjj/p/11077419.html