路線代碼:
Route::group(['middleware' => 'auth', 'prefix' => 'admin'], function(){
Route::resource('gallery', GalleryController::class);
});
我用來上傳檔案的表格:
<form action="{{ route('gallery.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" name="gallery_img" id="inputGroupFile01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
@error('gal_img')
<span class="text-danger">{{ $message }}</span>
@enderror
<div class="input-group-append">
<div class="col-sm-10" style="padding-left: 1px;">
<button type="submit" class="btn btn-dark">Save</button>
</div>
</div>
控制器代碼:
public function store(GalleryRequests $request)
{
$gal_img = $request->file('gallery_img');
$gal_file = date('YmdHi').$gal_img->getClientOriginalName();
$gal_img->move(public_path('upload/gallery'), $gal_file);
$save_path = 'upload/gallery/'.$gal_file;
Gallery::insert([
'gal_img' => $save_path
]);
$notification = array(
'message' => 'Slider Inserted Successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
請求檔案驗證:
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'gal_img' => 'required'
];
}
public function messages(){
return [
'gal_img.required' => 'Please Select an Image First',
];
}
選擇影像后嘗試保存時出現的錯誤:

試圖找出幾個小時以來我做錯了什么并且現在很沮喪,請幫助我解決這個問題。提前致謝。
uj5u.com熱心網友回復:
表單中的欄位已命名gallery_img,因此必須檢查名稱:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'gallery_img' => 'required'
];
}
public function messages()
{
return [
'gallery_img.required' => 'Please Select an Image First',
];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464305.html
上一篇:表單未在反應中提交
下一篇:表單資料未填寫React
