laravel 上载包含多个图像的多个表单

mctunoxg  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(85)

下面是我的刀片文件:

<form action="{{url('manager/store-milestone')}}" method="POST" enctype="multipart/form-data">
    <div id="dynamicForm">
        <input type="hidden" name="project_id" value="{{$project->id}}">
        @csrf
        <div class="form-row" id="remove-div">
            <div class="form-group col-md-6 mb-0">
                <label> Name</label>
                <div class="form-group">
                    <input type="text" value="" required
                           class="form-control" name="addmore[0][name]" id="name" placeholder="Enter Milestone Name">
                </div>
            </div>
            <div class="form-group col-md-6 mb-0">
                <label> Description</label>
                <div class="form-group">
            <textarea type="text" value="" required
                      class="form-control" name="addmore[0][description]" id="name" placeholder="Enter Description"></textarea>
                </div>
            </div>
            <div class="form-group col-md-6 mb-0">
                <label> From</label>
                <div class="form-group">
                    <input type="datetime-local" value="" required class="form-control" name="addmore[0][from]" id="from">
                </div>
            </div>
            <div class="form-group col-md-6 mb-0">
                <label> To</label>
                <div class="form-group">
                    <input type="datetime-local" value="" required class="form-control" name="addmore[0][to]" id="to">
                </div>
            </div>
            <div class="form-group col-md-12 mb-0">
                <label> File</label>
                <div class="form-group">
                    <input type="file" class="form-control" name="addmore[0][file]" id="file" multiple>
                </div>
            </div>

        </div>
        <button type="button" name="add" id="add" class="btn btn-success ml-9"><i class="fa fa-plus"></i></button>

    </div>
    <button type="submit" class="btn btn-primary pull-right">Save</button>
    <a href="{{url('manager/project-list')}}" type="button"
       class="btn btn-secondary pull-right mr-4">Back</a>
</form>

下面是我的Jquery:

var i = 0;
$("#add").click(function () {
    ++i;

    $("#dynamicForm").append('<div class="form-row" id="remove-div"><div class="form-group col-md-6 mb-0"><label> Name</label><div class="form-group"><input type="text" name="addmore[' + i + '][name]" placeholder="Enter Milestone Name" class="form-control" /></div></div><div class="form-group col-md-6 mb-0"><label> Description</label><div class="form-group"><textarea type="text" name="addmore[' + i + '][description]" placeholder="Enter description" class="form-control" ></textarea></div></div><div class="form-group col-md-6 mb-0"><label> From</label><div class="form-group"><input type="datetime-local" value="" required class="form-control" name="addmore[' + i + '][from]" id="from"></div></div><div class="form-group col-md-6 mb-0"><label> To</label><div class="form-group"><input type="datetime-local" value="" required class="form-control" name="addmore['+ i +'][to]" id="to"></div></div><div class="form-group col-md-12 mb-0"><label> File</label><div class="form-group"><input type="file" class="form-control" name="addmore['+ i +'][file]" id="file" multiple><div class="mt-5 ml-9"><button type="button" class="btn btn-danger remove-form"><i class="fa fa-minus"></i></button></div></div>');

});

$(document).on('click', '.remove-form', function () {
    $(this).parents('#remove-div').remove();
});

这是我的控制器:

public function store(Request $request){
    $request->validate([
        'project_id' => 'required',
        'addmore.*.name' => 'required',
        'addmore.*.description' => 'required',
        'addmore.*.from' => 'required',
        'addmore.*.to' => 'required',
        'addmore.*.file.*' => 'nullable|file|max:10240',
    ]);

    $project_id = $request->project_id;
    $milestones = $request->addmore;

    foreach ($milestones as $index => $milestone) {
        $milestone['project_id'] = $project_id;
        $created_milestone = Milestone::create($milestone);

        if ($request->hasFile('addmore.' . $index . '.file')) {
            $files = $request->file('addmore.' . $index . '.file');
            foreach ($files as $file) {
                $fileName = rand(0, 999) . time() . '.' . $file->getClientOriginalExtension();
                $upload_path = public_path("upload/milestone");
                $file->move($upload_path, $fileName);
                $img_url = 'upload/milestone' . $fileName;
                MilestoneFile::create([
                    'manager_file' => $img_url,
                    'milestone_id' => $created_milestone->id
                ]);
            }
        }
    }
    return redirect()->back()->with('success', 'Milestones added successfully');
}

我想上载多个表单与多个图像。我有里程碑表与列名id,project_id,从,到,文件。另一个表里程碑_files与列名id,里程碑_id,经理_文件。我想插入多个表单与多个图像和图像存储在里程碑_files表。多个表单提交完美,但图像不能存储在里程碑_files。
请帮帮我。

2hh7jdfx

2hh7jdfx1#

foreach ($milestones as $index => $milestone) {
          $milestone['project_id'] = $project_id;
          $created_milestone = Milestone::create($milestone);

         if($request->hasFile('addmore.' . $index . '.file')) {
               $files = $request->file('addmore.' . $index . '.file');

                 foreach ($files as $key => $file) {
                    $fileName = time().'.'.$file->getClientOriginalExtension();
                    $path = Storage::disk('public_uploads')->putFileAs('milestones', $file, $fileName);
                    
                      $media = new MilestoneFile();
                      $media->milestone_id = created_milestone->id;
                      $media->manager_file = 'uploads/' . $path;
                      $media->save();
            }
         }
     }

你能试试这个吗?如果有用的话请告诉我

相关问题