Laravel Accessors 和 Mutators 不适用于 Camel Case 表字段名称

Yoh*_*oda 1 php accessor mutators laravel eloquent

我的表如下所示:

CREATE TABLE IF NOT EXISTS `ProductCategoryImage` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,  
  `productCategoryId` INT(11) NOT NULL,
  `imageName` VARCHAR(255) NOT NULL,  
  `thumbnailName` VARCHAR(255) NULL DEFAULT NULL,
  `location` TINYINT(2) NOT NULL DEFAULT 1,
  `status` TINYINT(2) NOT NULL DEFAULT 1,
  `createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deletedAt` DATETIME NULL DEFAULT NULL,
  `createdById` INT(11) NOT NULL DEFAULT -1,
  `updatedById` INT(11) NULL DEFAULT NULL,
  `deletedById` INT(11) NULL DEFAULT NULL
);
Run Code Online (Sandbox Code Playgroud)

在 ProductCategoryImage 模型中,我写下了以下两种方法:

public function getThumbnailNameAttribute($value)
{
    return self::getThumbnailUrl($value);    
}
public function setThumbnailNameAttribute($value)
{
    $this->attributes['thumbnailName'] = $value;
}
Run Code Online (Sandbox Code Playgroud)

Laravel 不会执行以上两种方法来自定义我的表字段值。

ProductCategoryImage 模型从自定义 BaseModel 扩展而来,BaseModel 从 Eloquent 扩展而来。

Laravel 没有像 beforeFind()、afterFind()、beforeSave()、afterSave() 这样的事件处理方法吗?

Yoh*_*oda 5

我的一名团队成员使用 toArray() 方法实现了它:

public function toArray()
{
    $toArray = parent::toArray();
    $toArray['thumbnailName'] = $this->thumbnailName;
    return $toArray;
}

public function getThumbnailNameAttribute($value)
{
    return self::getThumbnailUrl($value);
}
Run Code Online (Sandbox Code Playgroud)

像魅力一样工作。