Search code examples
laravel-backpack

Issue uploading files with PageManager


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);
    }

Solution

  • 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.