Laravel 8 - 我正在使用多输入文件,无法在不同行DB中存储多个图像

问题描述

我需要将多个图像和文本存储在不同的行中(在 DB 中)请帮助我提前致谢

        **Create Blade**
            <div class="card-header card-header-border-bottom " >
                <h2 class="text-center mt-2">Create Content</h2>
            </div>
            <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
                <script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                <form action="{{ route('Content.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf             
                    <div class="form-group col-md-6">
                        <label for="exampleFormControlTextarea1">Image</label>
                        <input type="file" name="image[]" class="form-control" id="exampleFormControlInput1"
                            placeholder="Enter Contact phone">
                    </div>        
                    <button id="removeRow"  type="button" class="btn btn-danger">Remove Program</button>
                    <div id="newRow"></div>
                    <button id="addRow" type="button" class="btn btn-warning mb-5">Add Program</button>   
                    </div>
                    <div class="row">
                        <div class="col-lg-12 mb-5">
                            <button   type="submit" class="btn btn-primary btn-default">Submit</button>
                        </div>
                    </div>
                </form>
   

     <script type="text/javascript">
            $("#addRow").click(function () {
                var html = '';
                html += '<div class="form-group col-md-6">';
                html += '<label for="exampleFormControlTextarea1">Image</label>';
                html += '<input type="file" name="image[]" class="form-control" id="exampleFormControlInput1">';
                html += '</div>';
                html += '<button id="removeRow" type="button" class="btn btn-danger">Remove Program</button>';
                $('#newRow').append(html);
            });
            $(document).on('click','#removeRow',function () {
                $(this).closest('#inputFormRow').remove();
            });
        </script>
**Controller**
     public function store(Request $request)
            {
                if($request->hasfile('image'))
                 {
                    foreach($request->file('image') as $image)
                    {
                        $name=$image->getClientOriginalName();
                        $image->move(public_path().'/image/content/',$name);  
                        $datas[] = $name;  
                    }
                 }
                foreach ($request->date as $key=> $date){
                    $data = new Content();
                    $data->date = $date;
                    $data->title = $request->title[$key];
                    $data->subtitle = $request->subtitle[$key];
                    $data->content = $request->content[$key];
                    $data->image = $request->image[$key];
                    $data->save();
                }
                return view('backend.content.index');
            }
    **Migration Table**
        
    
        public function up()
                    {
                        Schema::create('contents',function (Blueprint $table) {
                            $table->id();
                            $table->string('date');
                            $table->string('title');
                            $table->string('subtitle');
                            $table->text('content');
                            $table->string('image');
                            $table->timestamps();
                        });
                    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)