英文:
Issue uploading files with PageManager
问题
I'm providing the translation of the code you've shared:
我试图上传一张图片到背包 'extras' 的 fakeColumn 中。然而,在 mutator 的 $value 中没有任何内容,也没有上传图片。
我可以将其作为数据库列,但仅有一个页面类型将使用,感觉不合适。如果我将其更改为可填充列,mutator 就会突然接收到一个值。
我查看了其他问题,但似乎没有涵盖如何添加到虚拟列的情况?大多数似乎是与 mutator 名称有关的问题,但我不认为在这里是这样的情况?
我的 PageTemplates.php:
```php
$this->crud->addField([
'name' => 'image',
'label' => 'Image',
'type' => 'upload',
'upload' => true,
'disk' => 'public',
'store_in' => 'extras',
'fake' => true
]);
我的 Page.php 模型:
protected $table = 'pages';
protected $primaryKey = 'id';
public $timestamps = true;
protected $fillable = ['template', 'name', 'title', 'slug', 'content', 'extras'];
protected $fakeColumns = ['extras'];
protected $casts = ['extras' => 'array'];
...
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = "public";
$destination_path = "/images/pages";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
请注意,我已经翻译了代码部分,如有需要,请继续使用这个翻译。
英文:
I'm trying to upload to the backpack 'extras' fakeColumn an image. However nothing appears in the $value of the mutator, and no image is uploaded.
I could make this a column in the database but it feels wrong to create a database column only one page type will use. If I do change to a fillable column the mutator suddenly receives a value.
I've taken a look at other questions but none seem to cover adding to the fake column? most seem to be an issue with the mutator name, but I don't think that's the case here?
My PageTemplates.php:
$this->crud->addField([
'name' => 'image',
'label' => 'Image',
'type' => 'upload',
'upload' => true,
'disk' => 'public',
'store_in' => 'extras',
'fake' => true
]);
My Page.php model:
protected $table = 'pages';
protected $primaryKey = 'id';
public $timestamps = true;
protected $fillable = ['template', 'name', 'title', 'slug', 'content', 'extras'];
protected $fakeColumns = ['extras'];
protected $casts = ['extras' => 'array'];
...
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = "public";
$destination_path = "/images/pages";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
答案1
得分: 0
"Indeed, fake fields do not trigger individual mutators, they trigger the 'fake column' mutator.
In your case, instead of defining a setImageAttribute()
mutator, please define a setExtrasAttribute()
mutator. You'll have the $value
then."
英文:
Indeed, fake fields do not trigger individual mutators, they trigger the "fake column" mutator.
In your case, instead of defining a setImageAttribute()
mutator, please define a setExtrasAttribute()
mutator. You'll have the $value
then.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论