简体   繁体   中英

How to save an image in Laravel

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" class="form-control-file" 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

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",      
]);

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

enctype="multipart/form-data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM