I have not been able to upload images from my Laravel application, it saves NULL
or only the name of the image.
I think I am only referencing the filename and not the file itself.
In my HTML form I have the following: Note that this is an array of images.
<div class="designs">
<input class="design" type="file" name="subdesigns[image][]" accept="image/*" placeholder="Sube el diseño">
<label class="select">
<select name="subdesigns[spec_id][]">
<option value="">Formato</option>
@foreach($specs as $spec)
<option value="{{$spec->id}}">{{$spec->name}}</option>
@endforeach
</select>
</label>
</div>
And then in my SubdesignController
store as follows, for all the images that have been added:
//Substract array of the images
$subdesigns = $request->input('subdesigns.image');
//Substract array of the specs
$specs = $request->input('subdesigns.spec_id');
//Creations of subdesigns
$i = 0; //To move specs array
foreach ($subdesigns as $subs) {
$sub = new Subdesign();
$sub->image = $subs;
$sub->spec_id = $specs[$i]; //Adding the corresponding spec_id to each subdesign
$sub->design_id = $design->id;
$sub->save();
$i++; // Move to the next spec_id
}
Now in my model Subdesign
I have two mutators to save the images just like I had in a Backpack project, which works perfectly. But when I set everything up to work with this new project it saves only NULL
and when I comment out Mutators
it only saves the string
file name.
public function setImageAttribute($value)
{
$user = Auth::user();
$attribute_name = "image";
$disk = "public_folder";
$destination_path = "Disenos/" . $user->id;
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
// return $this->{$attribute_name}; // uncomment if this is a translatable field
}
public function uploadFileToDisk($value, $attribute_name, $disk, $destination_path)
{
$request = \Request::instance();
// if a new file is uploaded, delete the file from the disk
if (
$request->hasFile($attribute_name) &&
$this->{$attribute_name} &&
$this->{$attribute_name} != null
) { dd("1");
\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) {dd("2");
\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()) { dd("3");
// 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
$file_path = $file->storeAs($destination_path, $new_file_name, $disk);
// 3. Save the complete path to the database
$this->attributes[$attribute_name] = $file_path;
// $this->import($file_path);
}
}
I also have added the disk
en fylesystems.php
as follows:
<?php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
'public_folder' => [
'driver' => 'local',
'root' => public_path('images'),
'url' => '/images',
],
],
];
How can I solve this, I don't know what I'm doing wrong.
The best practice is to save the path of where it is saved, not the image itself. In the database it should appear, in the column for the image, for example
public/images/image1.jpg