多文件上传在Laravel Backpack中与子字段关联时无法正常工作。

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

Multiple Upload doesn't work in a relationship with subfields in laravel backpack

问题

在使用与相关模型CRUD相关的upload_multiple时,我遇到了问题。以下是我的解决方案:

CustomerCase模型的CaseComment关联中,你使用了一个setDocumentsAttribute mutator,该mutator在documents属性上触发文件上传。然而,在CustomerCaseCrudController中,你的字段配置中缺少关于触发该mutator的信息。

解决方法是,在CustomerCaseCrudController的字段配置中,为documents字段添加一个upload选项,将其设置为true。这将告诉CRUD系统在处理documents字段时触发mutator。

CustomerCaseCrudController的字段配置中,将以下代码:

$this->crud->addField(
    [
        'name'          => 'comments',
        'type'          => "relationship",
        'tab'           => 'Comments',
        'subfields'     => [
            [
                'name' => 'comment',
                'label' => 'Comments',
                'type' => 'textarea',

            ],
            [
                'name'      => 'documents',
                'label'     => 'Documents',
                'type'      => 'upload_multiple',
                'upload'    => true, // 添加这一行
                'disk'      => 'public'
            ],
        ]
    ]
);

通过将'upload' => true添加到documents字段的配置中,你告诉CRUD系统在上传文件时触发setDocumentsAttribute mutator。

请确保在更新代码后清除缓存,然后测试该功能。

英文:

have an issue with the upload_multiple when I try to use it from related model crud.

these are my models

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CaseComment extends Model
{
    use \Backpack\CRUD\app\Models\Traits\CrudTrait;
    use HasFactory;
    use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'comment',
        'customer_case_id',
        'important',
        'documents'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'important' => 'boolean',
    ];

    public function customerCase()
    {
        return $this->belongsTo(CustomerCase::class);
    }

    public function setDocumentsAttribute($value)
    {
        $attribute_name = "documents";
        $disk = "public";
        $caseCode = CustomerCase::find($this->attributes['customer_case_id']);
        $destination_path = "documents/".$caseCode->casecode;
        $this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
    }
}

<?php

namespace App\Models;

use App\Helpers\Helpers;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class CustomerCase extends Model
{
    use \Backpack\CRUD\app\Models\Traits\CrudTrait;
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'booking',
        'casecode',
        'status',
        'case_type_id',
        'user_id',
        'description',
        'resolution',
        'customer',
        'hotel_id',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'state' => 'integer',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function hotel()
    {
        return $this->belongsTo(Hotel::class);
    }

    public function casetype()
    {
        return $this->belongsTo(CaseType::class,'case_type_id');
    }

    public function comments(){
        return $this->hasMany(CaseComment::class);
    }

    /**

     * Set the post title.

     *

     * @param string $value

     * @return string

     */

    public function setCasecodeAttribute($value)
    {
        $this->attributes['casecode'] = $this->attributes['booking'].Carbon::parse($this->attributes['created_at'])->timestamp;
    }


}

In my CustomerCaseCrudController I have this field:

$this->crud->addField(
            [
                'name'          => 'comments',
                'type'          => "relationship",
                'tab'           => 'Comments',
                'subfields'   => [
                    [
                        'name' => 'comment',
                        'label' => 'Comments',
                        'type' => 'textarea',

                    ],
                    [
                    'name'      => 'documents',
                    'label'     => 'Documents',
                    'type'      => 'upload_multiple',
                    'upload'    => true,
                    'disk'      => 'public'
                    ],
                    ]
            ]
        );

If I try to create a comment from the CustomerCase crud, the comment is created but the file is not uploaded and the path is not stored in the DB.

The I created a crud for the CaseComment in order to test and I added exactly the same upload_multiple field

CRUD::addField([   // Upload
            'name'      => 'documents',
            'label'     => 'Documentos',
            'type'      => 'upload_multiple',
            'upload'    => true,
            'disk'      => 'public'
        ]);

and works perfectly. So the mutator it's working, Also I can get the relation. Why it's not working from the CustomerCase crud?

答案1

得分: 1

谢谢提出问题。这是一个已知的问题,正在这里处理:https://github.com/Laravel-Backpack/CRUD/pull/4921

我们预计最迟到本月底将其合并,因为我们对于文件上传有一些关于这个PR的惊喜 多文件上传在Laravel Backpack中与子字段关联时无法正常工作。

祝好

英文:

thanks for raising the question. That's a known issue that is beeing addressed here: https://github.com/Laravel-Backpack/CRUD/pull/4921

We expect by the end of the month at most have it merged as we have a few surprises for you guys regarding file uploads that goes along with that PR 多文件上传在Laravel Backpack中与子字段关联时无法正常工作。

Cheers

huangapple
  • 本文由 发表于 2023年3月9日 19:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684114.html
匿名

发表评论

匿名网友

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

确定