Devs.tw 是讓工程師寫筆記、網誌的平台。歡迎您隨手紀錄、寫作,方便日後搜尋!
找到一篇文章,才知道有這種寫法
https://hdtuto.com/article/laravel-57-image-upload-with-validation-example
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('images'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>上傳失敗!</strong>檔案出現以下問題:
<ul class="mb-0">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
才短短幾行,上傳、驗證、搬檔案通通都做到了