Home > Blockchain >  How to save an image in Laravel
How to save an image in Laravel

Time:10-11

I want to save the route of an image in the database. In my table I have an image field which is a string field.

And the code to save the image is this:

Question::create([
            'title' => $request->title,
            'slug' => $request->slug,
            'image' => Storage::put('imagenapp', $request->file('image')),
            'evaluation_id' => $request->evaluation_id,
            'type' => "OMI",
            
        ]);

The input in the view is this:

<input type="file" name="image" id="image"  accept="image/*">

I already have changed this line of code in filesystems.php file:

'default' => env('FILESYSTEM_DISK', 'public'),

And this line in the .env file:

FILESYSTEM_DISK=public

But when I try to store the data in the database I have the next error:

League\Flysystem\Filesystem::write(): Argument #2 ($contents) must be of type string, null given, called in C:\xampp\htdocs\aplicacionuno\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php on line 360

CodePudding user response:

Try this

create([
  'title' => $request->title,
  'slug' => $request->slug,
  'image' => $request->file('image')->storeAs('imagenapp', uniqid(rand()) . '.' .  $request->image->extension()),
  'evaluation_id' => $request->evaluation_id,
  'type' => "OMI",      
]);

CodePudding user response:

I found the mistake, I missed this code in the form:

enctype="multipart/form-data"
  • Related