[英]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:我已经在 filesystems.php 文件中更改了这行代码:
'default' => env('FILESYSTEM_DISK', 'public'),
And this line in the.env file: .env 文件中的这一行:
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.