我正在尝试使用 laravel 上传多张图片,到目前为止我已经成功了,但问题是当我尝试保存文本和文件时出现错误。到目前为止,我收到了很多错误,我记不清了,但最新的是:
SQLSTATE [HY000]:常规错误:1364字段'标题'没有默认值(SQL:INSERT INTO
posts
(image
,updated_at
,created_at
)值( “italian.jpg”],2019年5月23日18时48分22秒, 2019-05-23 18:48:22))
我已经正确设置了 sql,正如我所说,如果我删除图像上传它可以工作,如果我删除文本字段它也可以工作,但如果我同时尝试,我不会。如果我没记错的话,当我删除必填字段时它也可以工作。
public function store(Request $request) { //dd($request);
$this->validate($request, [
'title' => 'required|min:3|max:120',
'text' => 'required',
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if($request->hasfile('image')){
foreach($request->file('image') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
} else{
redirect('/posts')->with('Error', 'no image');
}
$post->image=json_encode($data);
$post = Post::create($validatedData);
return redirect('/posts')->with('success', 'yay');
}
我也试过这个,但它返回
从空值创建默认对象
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|min:3|max:120',
'category' => 'required|min:3|max:120',
'text' => 'required',
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if($request->hasfile('image'))
{
foreach($request->file('image') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$post = new Post();
$post->image=json_encode($data);
$post->save();
return back()->with('success', 'Yay');
}
我想通了,嘿嘿。只需要删除这些行:
'image' => 'required', 'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
无论如何谢谢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句