微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。