I am using free version of Backpack 5 for Laravel as my admin panel. Also, I am using Laravel 10.13 and PHP 8.2.6. I have been trying to create a feature to add pictures for my blog posts however whenever I save the file it shows in my database following path: C:\wamp64\tmp\phpEF80.tmp However, I need my files to be saved in public/images/news. Please could you help me to solve this problem? I have googled and I could not find even a similar solution. Mainly people have problems with wrong directory, or they cannon open files, but it seems no one had probles with saving files into temporary directory of webserver. Restarting server and cleaning cache or running php artisan optimize
does not help. Using public as disk in Controller and Model also create the same issue.
MIGRATION FILE
Schema::create('news', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('article');
$table->string('main_picture_path');
$table->timestamps();
});
MODEL
use CrudTrait;
use HasFactory;
protected $fillable = [
'title',
'article',
'main_picture_path'
];
public function setImageAttribute($value)
{
$attribute_name = "main_picture_path";
$disk = "images";
$destination_path = "news";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
CRUD CONTROLLER
protected function setupCreateOperation()
{
CRUD::setValidation(NewsRequest::class);
CRUD::field('title');
CRUD::field('article');
$this->crud->addField([
'name' => 'main_picture_path',
'label' => 'main_picture_path',
'type' => 'upload',
'upload' => true,
'disk' => 'images',
]);
}
FILESYSTEMS
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
'images' => [
'driver' => 'local',
'root' => public_path('images'),
],
]
I need my files to be saved in public/images/news. I have googled and I could not find even a similar solution. It seems no one had problems with saving files into temporary directory of webserver. Restarting server and cleaning cache or running php artisan optimize
does not help. Using public as disk in Controller and Model also create the same issue. Also, I tried to write simple debugging code in my modal in the method of setImageAttribute but it did not work at all, nothing was shown as output:
$attribute_name = "main_picture_path";
if (!request()->hasFile($attribute_name)) {
throw new \Exception('No file was uploaded');
}
if (!request()->file($attribute_name)->isValid()) {
throw new \Exception('Uploaded file is not valid');
}
throw new \Exception('IT SHOULD WORK');
A mutator should have attribute name in the method name. In my case I called the mutator as setImageAttribute and my attribute name was main_picture_path so in order for everything to work flawlessly the mutator should be named as follows: setMainPicturePathAttribute. Nothing was said about it in the documentation and hours of hard work helped me to find out the solution.