Php 如何使用其他表单实现dropzone并将post_id插入到图像表中?

Php 如何使用其他表单实现dropzone并将post_id插入到图像表中?,php,laravel,stored-procedures,file-upload,dropzone.js,Php,Laravel,Stored Procedures,File Upload,Dropzone.js,所以我有一个包含表单和上传图像的页面,数据库中有两个表 图像表 产品表 所有表格都在产品表中,对于图片,我将它们放在图像表中。。通过添加产品标识作为外键产品标识 没有使用dropzone,一切都可以用通常的输入法顺利进行。。但是当使用dropzone时,它是不起作用的 <form method="POST" action="{{ route('products.store') }}" id="upload" role=

所以我有一个包含表单和上传图像的页面,数据库中有两个表

图像表

产品表

所有表格都在产品表中,对于图片,我将它们放在图像表中。。通过添加产品标识作为外键产品标识

没有使用dropzone,一切都可以用通常的输入法顺利进行。。但是当使用dropzone时,它是不起作用的

    <form method="POST" action="{{ route('products.store') }}" id="upload" role="form" enctype="multipart/form-data">
                @csrf
    
    <div id="previewsContainer" class="dropzone">
                                    <div class="dz-default dz-message">
                                        <button class="dz-button" type="button">
                                            Drop files here to upload
                                        </button>
                                    </div>
                                </div>
    
// this is my other field

    <div class="grid grid-cols-3 gap-6">
                                    <div class="col-span-6">
                                        <label for="nama produk" class="block text-sm font-medium text-gray-700">Nama Produk</label>
                                        <input type="text" name="nama" id="nama produk" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"/>
                                        @if($errors->has('nama'))
                                            <span class="float-right text-danger">{{ $errors->first('nama') }}</span>
                                        @endif
                                    </div>
                                </div>
            
                  .. other field
               
                                <button type="submit" id="dz-submit" class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                                    Simpan
                                </button>
                            </div>
            

    </form>
//

这是我的产品路线和商店功能:

Route::resource('products', ProductController::class);

public function store(Request $request): RedirectResponse
    {
      
        $nama=$request->nama;
        $harga=$request->harga;
        $kategori_id=$request->kategori_id;
        $deskripsi=$request->deskripsi;

        $product  = new Product;
        $product -> nama=$nama;
        $product -> harga=$harga;
        $product -> kategori_id=$kategori_id;
        $product -> deskripsi=$deskripsi;
        $product -> user_id=Auth::user()->id;
        $product -> save();

        $productId=$product->id;

        if ($request->hasfile('file')) {
            $files = $request->file('file');

            foreach($files as $file) {
                $name   = time().'.'.$file->getClientOriginalName();
                $path   = public_path('/storage/product-image');
                $file  -> move($path, $name);

                // HERE .. Iam trying to insert the product id and other image data

                $image -> product_id=$productId;
                $image -> image_path=$name;
                $image -> save();


               // also try this

                Image::create([
                    'product_id' => $productId,
                    'image_path' => $name,
                    'image_order' => Auth::user()->id
                ]);
                
            }
        }

        return redirect('/products')->with("success"," files uploaded successfully");
    }
顺便说一句,所有图像数据都是可以为null的,所以如果图像顺序为null则无关紧要

我尝试了这段代码,所有的产品数据成功地插入到数据库,然后图像成功地上传到服务器。。但是图像数据没有存储到数据库中。。这是我的问题

如有任何帮助,我们将不胜感激,提前谢谢

Route::post('/dropzone', [ProductController::class,'dropzone'])->name('dropzone');

// save images to server 

public function dropzone(Request $request){
       
        $file = $request->file('file');
        $name   = time().'.'.$file->getClientOriginalName();
        $path   = public_path('/storage/product-image');
        $file  -> move($path, $name);

    }
Route::resource('products', ProductController::class);

public function store(Request $request): RedirectResponse
    {
      
        $nama=$request->nama;
        $harga=$request->harga;
        $kategori_id=$request->kategori_id;
        $deskripsi=$request->deskripsi;

        $product  = new Product;
        $product -> nama=$nama;
        $product -> harga=$harga;
        $product -> kategori_id=$kategori_id;
        $product -> deskripsi=$deskripsi;
        $product -> user_id=Auth::user()->id;
        $product -> save();

        $productId=$product->id;

        if ($request->hasfile('file')) {
            $files = $request->file('file');

            foreach($files as $file) {
                $name   = time().'.'.$file->getClientOriginalName();
                $path   = public_path('/storage/product-image');
                $file  -> move($path, $name);

                // HERE .. Iam trying to insert the product id and other image data

                $image -> product_id=$productId;
                $image -> image_path=$name;
                $image -> save();


               // also try this

                Image::create([
                    'product_id' => $productId,
                    'image_path' => $name,
                    'image_order' => Auth::user()->id
                ]);
                
            }
        }

        return redirect('/products')->with("success"," files uploaded successfully");
    }