laravel - Laravel:无法选择要上传的多个图像
问题描述
我有一个可以将图像上传到数据库的表单。我可以选择 1 张图片并上传。我的代码运行良好,但我想更新我的代码,以便它可以选择多个图像而不是一个。
任何帮助表示赞赏,谢谢。
控制器文件:
public function store(Request $request){
//image validation
$animal = $this->validate(request(), [
'image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:500',
]);
//Handles the uploading of the image
if ($request->hasFile('image')){
//Gets the filename with the extension
$fileNameWithExt = $request->file('image')->getClientOriginalName();
//just gets the filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//Just gets the extension
$extension = $request->file('image')->getClientOriginalExtension();
//Gets the filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Uploads the image
$path = $request->file('image')->storeAs('public/images', $fileNameToStore);
}
else {
$fileNameToStore = 'noimage.jpg';
}
}
刀片文件:
<form method ="post" action="/animals" enctype="multipart/form-data">
@csrf
<label for="name">Enter Name:</label>
<input type="text" id="name" name="name" required/>
<label for="breed">Which animal are you adding to the system?</label>
<select id="breed" name="breed">
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="rabbit">Rabbit</option>
<option value="hamster">Hamster</option>
<label for="dob">Enter Date of Brith:</label>
<input type="date" id="dob" name="dob" required>
<label for="available">Availability:</label>
<input type="text" id="available" name="available" required>
<label for="description">Description:</label>
<input type="text" id="description" name="description" required>
<label>Please Select Images To Upload:</label>
<input type="file" name="images[]" multiple placeholder="Select multiple files"/>
<input type="submit" value="Submit">
</form>
模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Animal extends Model
{
protected $table = 'animal';
}
解决方案
完成此任务的一种方法如下所示。您可以将其作为示例代码:
移民:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Animals extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('animals', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('animals');
}
}
模型动物
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class animal extends Model
{
use HasFactory;
protected $fillable = ['name'];
}
控制器的存储方法
<?php
namespace App\Http\Controllers;
use App\Models\animal;
use Illuminate\Http\Request;
class FrontController extends Controller
{
public function index(){
//return a view over here for your index
/*For Example: return view('front.animal'); where front is your folder and inside your folder there's animal.blade.php */
}
public function store(Request $request)
{
$animal = $this->validate(request(), [
'image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:500',
]);
if ($request->hasfile('images')) {
$images = $request->file('images');
foreach($images as $image) {
$filename = $image->getClientOriginalName();
//$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extension = $image->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $image->storeAs('public/images', $fileNameToStore);
Animal::create([
'animal' => $fileNameToStore,
]);
}
}
return back()->with('success', 'Images uploaded successfully');
}
}
刀片模板:
<input type="file" name="images[]" multiple class="form-control" accept="image/*">
推荐阅读
- javascript - 如何使用虚拟现实 Javascript 库 - Marzipano?
- javascript - Django Chart.js ajax javascript“字符串数据解析错误”
- math - CIELab 到 CIELCh(ab) 的转换没有产生正确的结果
- python - python double for循环属性问题
- excel - 创建 Excel 工作簿副本并删除查询连接
- c# - 使用 LINQ 从 ListBox 填充 ComboBox
- javascript - 制作移动键盘时遇到问题
- r - Use a list of subset indices in a for loop or function
- python-2.7 - 有没有办法在 python 2.7 中使用通配符搜索 python 字典?
- r - ggplot sec_axis 我可以为反式公式使用向量吗?