问题:使用PageManager上传文件时出现问题。

huangapple go评论34阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月18日 08:05:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定