英文:
Why upload image fails using Laravel 9 CRUD?
问题
我正在为图书创建一个数据库。当我测试没有图像上传的CRUD(普通CRUD)时,它是有效的。但是,当我将书籍封面图片文件作为数据输入的一部分添加后,我收到了错误消息“参数#1($rules)必须是数组类型,给定Illuminate\Http\Request,调用在”。我不理解是什么导致了这个错误,是因为验证还是图像文件夹的路径问题?
Model/Buku.php
class Buku extends Model
{
use HasFactory;
protected $fillable = [
'cover',
'nama_buku',
'author',
'terbitan',
'barcode',
'ketersediaan',
];
}
我怀疑错误出现在控制器代码中,但不知道为什么普通CRUD成功而图像上传CRUD失败。
Controllers/BukuController.php
protected function store(Request $request)
{
$request->validate($request, [
'cover' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'nama_buku' => 'required',
'author' => 'required',
'terbitan' => 'required',
'barcode' => 'required',
'ketersediaan' => 'required',
]);
$input = $request->all();
if ($cover = $request->file('cover')) {
$destinationPath = 'cover/';
$profileImage = date('YmdHis') . "." . $cover->getClientOriginalExtension();
$cover->move($destinationPath, $profileImage);
$input['cover'] = "$profileImage";
}
Buku::create($input);
return redirect()->route('buku.index')->with('success','Buku has been created successfully.');
}
我不知道错误实际上是否来自输入,所以以防万一,这是视图创建模板。
Views/create.blade.php
<form action="{{ route('buku.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cover:</strong>
<input type="file" name="cover" placeholder="Cover Buku"
class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" />
@error('cover')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<!-- 其他输入字段 -->
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
我还尝试在仍在使用Laravel 4时上传图像代码,仍然收到相同的错误。有人知道如何修复这个问题吗?
英文:
I am creating a database for books. When I was testing CRUD without image upload(normal CRUD), it was working. But when I added book cover image file as part of data input I get error "
Argument #1 ($rules) must be of type array, Illuminate\Http\Request given, called in
". I didn't understand what caused it, is it because validation or path image folder will be stored in?
Model/Buku.php
class Buku extends Model
{
use HasFactory;
protected $fillable = [
'cover',
'nama_buku',
'author',
'terbitan',
'barcode',
'ketersediaan',
];
}
I suspect the error is in controllers code but didn't know why normal CRUD succeed but image upload CRUD fails.
Controllers/BukuController.php
protected function store(Request $request)
{
$request->validate($request, [
'cover' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'nama_buku' => 'required',
'author' => 'required',
'terbitan' => 'required',
'barcode' => 'required',
'ketersediaan' => 'required',
]);
$input = $request->all();
if ($cover = $request->file('cover')) {
$destinationPath = 'cover/';
$profileImage = date('YmdHis') . "." . $cover->getClientOriginalExtension();
$cover->move($destinationPath, $profileImage);
$input['cover'] = "$profileImage";
}
Buku::create($input);
return redirect()->route('buku.index')->with('success','Buku has been created successfully.');
}
I don't know if the errors is actually from input so just in case here is views create blade
Views/create.blade.php
<form action="{{ route('buku.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cover:</strong>
<input type="file" name="cover" placeholder="Cover Buku"
class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100" />
@error('cover')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nama Buku:</strong>
<input type="text" name="nama_buku" class="form-control" placeholder="Nama Buku">
@error('nama_buku')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Author:</strong>
<input type="text" name="author" class="form-control" placeholder="Author Buku">
@error('author')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Terbitan:</strong>
<input type="text" name="terbitan" class="form-control" placeholder="Terbitan Buku">
@error('terbitan')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Barcode:</strong>
<input type="text" name="barcode" placeholder="Barcode Buku" id="scanner" />
@error('barcode')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Ketersediaan:</strong>
<input type="number" name="ketersediaan" class="form-control" placeholder="Ketersediaan Buku">
@error('ketersediaan')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
I also tried image upload code when I was still using Laravel 4, still get the same error. Did anyone know how to fix this?
答案1
得分: 0
The error states it pretty clearly. You pass a Request
object where an array is expected.
Instead of using the object directly, you should use the all()
method, which creates an array with all the posted fields in your request. Or in this case, you can leave the request->all()
out because the method is already called on the request object.
$request->validate($request,...)
$request->validate($request->all(),...)
$request->validate(['cover' => ....])
#########EDIT
It is also not recommended to insert all request data. You should only insert validated data.
$input = $request->all();
Buku::create($input);
You should use one of the two following approaches to only insert validated data: Laravel Documentation
英文:
The error states it pretty clearly. You pass a Request
object where an array is expected.
Instead of using the object directly, you should use the all()
method, which creates an array with all the posted fields in your request. Or in this case, you can leave the request->all()
out, because the method is already called on the request object.
$request->validate($request,...)
$request->validate($request->all(),...)
$request->validate(['cover' => ....])
#########EDIT<br>
It is also not recommended to insert all request data. You should only insert validated data.
$input = $request->all();
Buku::create($input);
You should use one of the two following approaches to only insert validated data: https://laravel.com/docs/10.x/validation#working-with-validated-input
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论