Hi @tabacitu i spend this night to try to resolve this but...
//Product.php
<?php
namespace app\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations; // ;))))
class Product extends Model
{
use CrudTrait;
use Sluggable, SluggableScopeHelpers;
use HasTranslations; // ;))))
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
//protected $table = 'products';
protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['name','slug','pdf','image','title', 'content', 'status', 'category_id', 'featured', 'date'];
// protected $hidden = [];
// protected $dates = [];
// protected $fakeColumns = ['pdf'];
protected $translatable = ['name','title', 'content','pdf'];
protected $casts = [
'featured' => 'boolean',
'date' => 'date',
// 'pdf' => 'array',
];
public function setPdfAttribute($value)
{
$attribute_name = "pdf";
$disk = "pdf";
$destination_path = "pdf";
$this->uploadPdfToDisk($value, $attribute_name, $disk, $destination_path);
}
public function uploadPdfToDisk($value, $attribute_name, $disk, $destination_path)
{
$request = \Request::instance();
// dd($request);
// if a new file is uploaded, delete the file from the disk
if ($request->hasFile($attribute_name) &&
$this->{$attribute_name} &&
$this->{$attribute_name} != null) {
\Storage::disk($disk)->delete($this->{$attribute_name});
$this->attributes[$attribute_name] = null;
}
// if the file input is empty, delete the file from the disk
if (is_null($value) && $this->{$attribute_name} != null) {
\Storage::disk($disk)->delete($this->{$attribute_name});
$this->attributes[$attribute_name] = null;
}
// if a new file is uploaded, store it on disk and its filename in the database
if ($request->hasFile($attribute_name) && $request->file($attribute_name)->isValid()) {
// 1. Generate a new file name
$file = $request->file($attribute_name);
$new_file_name = md5($file->getClientOriginalName().time()).'.'.$file->getClientOriginalExtension();
// 2. Move the new file to the correct path
// dd($destination_path);
$file_path = $file->storeAs("/", $new_file_name, $disk);
// dd($file_path);
// 3. Save the complete path to the database
// $this->attributes[$attribute_name] = $file_path;
$this->attributes[$attribute_name] = $destination_path.'/'.$new_file_name;
}
}
//ProductCrudController.php
$this->crud->addField([ // pdf
'label' => "pdf",
'name' => "pdf",
'type' => 'upload',
'upload' => true,
]);
The result in DB

pdf upload works but when i try to make it translatable does not work :(
I need help
Thanks ;)
Hi @lloy0076 !!
i think it's the same problem of #894
Any suggestion?
Actually I do think it is the same bug.
Duplicate of #894.
SOLVED!
Upgrade these 2 files first:
The setTranslation function search a $value returned from the Mutator,
so you have to return the $value of the file you have uploaded ;)
public function setPdfAttribute($value)
{
$attribute_name = "pdf";
$disk = "pdf";
$destination_path = "pdf";
$this->uploadPdfToDisk($value, $attribute_name, $disk, $destination_path);
// OH YEAH BABY!!!
return $this->attributes['pdf'];
}
@meodev - are you able to turn your changes into a PR (I can't figure out what you changed easily)?
@lloy0076 - these 2 links are better explained than the previous 2
I update these 2 files (but i don't know if is it necessary)
The value of the translatable field was always null because the Mutator setPdfAttribute
does not return anything. i.e. {"it":null}
Now return the path of the uploaded file and it works!
I think that a (possible) solution is to update the docs for the upload field
https://laravel-backpack.readme.io/v3.0/docs/crud-fields#section-upload
and say that if you have a mutator for a translatable field you have to return the path of the uploaded file from the mutator ;)
Just add
return $this->attributes['pdf'];
at the end of the Mutator where the 'pdf' is the attribute name
I never do a PR and i've got little time...but if you help me i can do it! ;)
@tabacitu
I think the solution is to:
I think that a (possible) solution is to update the docs for the upload field
https://laravel-backpack.readme.io/v3.0/docs/crud-fields#section-upload
and say that if you have a mutator for a translatable field you have to return the path of the uploaded file from the mutator ;)
Closing because there is a solution.