程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Laravel:无法选择多张图片上传大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Laravel:无法选择多张图片上传?

开发过程中遇到Laravel:无法选择多张图片上传的问题如何解决?下面主要结合日常开发的经验,给出你关于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\scheR_463_11845@a\Blueprint;
use Illuminate\Support\Facades\scheR_463_11845@a;

class Animals extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        scheR_463_11845@a::create('animals',function (Blueprint $tablE) {
            $table->id();
            $table->String('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        scheR_463_11845@a::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'];
}

Controller 的 store 方法

<?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/*">

大佬总结

以上是大佬教程为你收集整理的Laravel:无法选择多张图片上传全部内容,希望文章能够帮你解决Laravel:无法选择多张图片上传所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: