theUnforgiven's avatar

Dropzone JS question

Hi guys,

I'm using DropzoneJS to upload multiple images, but the page this is on is for products and the route assigned, doesn't know the product ID yet as that doesn't happen until the user clicks save.

Routes

post('products/images', 'ProductsController@images');
resource('products', 'ProductsController');

ProductRepo:

 public function createNewProduct($request)
    {
        $product = new Products;
        $product->product = $request->input('product');
        $product->description = $request->input('description');
        $product->sku = $request->input('sku');
        $product->price = $request->input('price');
        $product->tax = $request->input('tax');
        $product->shipping = $request->input('shipping');
        $product->status = $request->input('status');
        $product->save();

        return $product;
    }

Controller:

public function store(NewProductRequest $request, EloquentProduct $product)
    {
        $product->createNewProduct($request);
        alert()->success('Success', 'Product successfully created');
        return redirect('admin/products');
    }

Then I have this method for the image uploading part. This is the part I'm not sure on how to save it the database without knowing the newly created product ID.

public function images(Request $request)
    {
        $file = $request->file('file');
        $name = time() . $file->getClientOriginalName();
        $file->move('uploads/products', $name);

       // Not sure what to do here to save the images to to the images db and assign 
       // the correct newly created product ID
    }

Any help greatly appreciated.

0 likes
5 replies
theUnforgiven's avatar

Also calling the dropzone on a div with a ID of dropzone

 <script>
        $(document).ready(function () {
            $("div#dropzone").dropzone({ url: "/admin/products/images" });
        });
    </script>
chaibialaa's avatar

1 - Rather than replying to yourself, try editing your own question.

2- Include your upload part in the product adding, then include the id like : $product->id

Here is sample which I'm using right while replying to you :

$ClassData = Classm::create([
            'title' => $data['title']
        ]);

        DB::table('level_classes')->insert(
            ['level_id' => $data['level'],
             'class_id' => $ClassData->id ]);

In my case, I add a class, and then do the attach of the newly created (which I don't know the proper ID) at a new database entry in a different table.

simondavies's avatar

I use it on one way where I have a dropdown that has a list of images from my selected images folder. If there is no image there for the current entry then I have a link next to the drop down that will take the user to an image upload form.

The user can then upload the images required, and once completed its diverts back to the creation page. And the newly added images are now in the dropdown for selection.

https://www.dropbox.com/s/l9mpf578yb0yhjy/image-dropdown.png

Another way I have done this is similar to the above and instead of being taken to another page its done in a pop up, and on completion and the pop up closes, JS is then used to refresh the dropdown list, so the user does not leave the actual page, especially if they have added content already.

Please or to participate in this conversation.