Good afternoon mates.
I'm trying to save an image and I get the following error
League\Flysystem\Local\LocalFilesystemAdapter::__construct():
Argument #1 ($location) must be of type string, null given, called in C:\laragon\www\puntodeventa\vendor\laravel\framework\src\
Illuminate\Filesystem\FilesystemManager.php on line 183
How am I saving it? So I made a function in the base Controller
Controller:
?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Str;
use Image, Config;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function postFileUpload($field, $request, $thumbnails = null)
{
$path = date('Y/m/d');
$original_name = $request->file($field)->getClientOriginalName();
$final_name = Str::slug($request->file($field)->getClientOriginalName() . '_' . time()) . '.' . trim($request->file($field)->getClientOriginalExtension());
if ($request->$field->storeAs($path, $final_name, 'uploads')):
$data = json_encode(['upload' => 'success', 'path' => $path, 'original_name' => $original_name, 'final_name' => $final_name]);
else:
$data = ['upload' => 'error'];
endif;
if ($thumbnails):
$file_path = Config::get('filesystems.disks.uploads.root') . '/' . $path . '/' . $final_name;
foreach ($thumbnails as $thumbnail):
$img = Image::make($file_path)->orientate();
$img->fit($thumbnail[0], $thumbnail[1], function ($constraint) {
$constraint->aspectRatio();
});
$img->save(Config::get('filesystems.disks.uploads.root') . '/' . $path . '/' . $thumbnail[2] . '_' . $final_name, 75);
endforeach;
endif;
return $data;
}
public function getFileDelete($disk, $file, $thumbnails = null)
{
$end_file = json_decode($file, true);
$file_path = Config::get('filesystems.disks.' . $disk . '.root') . '/' . $end_file['path'] . '/' . $end_file['final_name'];
if (file_exists($file_path)):
unlink($file_path);
if ($thumbnails):
foreach ($thumbnails as $thumbnail):
$thumbnail_path = Config::get('filesystems.disks.' . $disk . '.root') . '/' . $end_file['path'] . '/' . $thumbnail . '_' . $end_file['final_name'];
if (file_exists($thumbnail_path)):
unlink($thumbnail_path);
endif;
endforeach;
endif;
endif;
}
}
ProductController
public function store(StoreProductRequest $request)
{
$product = Product::create($request->all()+
[
'image' => $this->postFileUpload('image', $request, [[256,256,'256x256']]),
]);
return redirect()->route('product.index');
}
file systems
'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,
],
'uploads' => [
'driver' => 'local',
'root' => config('puntodeventa.server_uploads_paths'),
'visibility' => 'public',
],
'uploads_user' => [
'driver' => 'local',
'root' => config('puntodeventa.server_uploads_user_paths'),
],
'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'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
],
point of sale
<?php
return [
'server_uploads_paths' => 'C:\laragon\www\puntodeventa\public\uploads',
'server_uploads_user_paths' => 'C:\laragon\www\puntodeventa\public\uploads_users',
'server_webapp_path' => 'c:/www/www/puntodeventa_pwa/',
]
?>
In fact, it saves an array in my IMAGE field that is type text in the BBDD, where the route is and other things that I will use later.
I have another project where it works fine, I don't understand why it gives me that error and it had never given me that error in the other project, thanks in advance for any suggestion you can give me
Well after hours of researching and trying here and trying there, I leave you my solution for future programmers :D
My error was in the filesystem.php of the config folder
I don't know why I wasn't taking the
config('puntodeventa.server_uploads__paths')
But I managed to solve it by placing the path in the .env as I leave here.I also modified the controller and I stay like this:
What is all that code for? To use in a few lines in the controller the function to save the images and their thumbnails
So I stay in the .env
I hope future programmers will help.