英文:
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的惊喜
祝好
英文:
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
Cheers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论