twg_'s avatar
Level 6

UIKit Upload on form

I'm using UIKit with a project I'm working on and I was wanting to use the uploader that ships with it. I've also thought about Dropzone.js, so if anyone can help me with either would be great.

I have a form created that has 5 fields (title, status, approved, details, images). The only field that has the upload assigned to it is my images field.

I want the user to fill out the form and then select images to upload (max of 3) and on submit, the title, status, approved, and details are written to one table and then the images are written to a secondary table with a table1_id written to each image based on the first table entry.

0 likes
7 replies
twg_'s avatar
Level 6

@bobbybouwmann Any other tutorials that don't require me to purchase a membership for just one video?

twg_'s avatar
Level 6

@bobbybouwmann Well my biggest issue is I'm trying to figure out how to do the file upload as part of the form submission. Currently I'm trying to loop through it on a foreach but it gives me an error on argument 2 on foreach.

public function store(Request $request)
    {

        $item = new Item;
        $item->hash = str_random(30);
        $item->active = $request->input('active');
        $item->user_id = Auth::id();
        $item->title = $request->input('title');
        $item->description = $request->input('description');
        $item->save();

        $files = $request->input('images');
        $file_count = count($files);
        $uploadCount = 0;

        foreach($files as $file)
        {
            $rules = array('file' => 'required|mimes:png,gif,jpeg,svg');
            $validator = Validator::make(array('file' => $file), $rules);
            if($validator->passes())
            {
                $destinationPath = 'uploads';
                $filename = $proof->id . '.' . $request->getClientOriginalName();
                $upload_success = $file->move($destinationPath, $filename);

                $image = new Image;
                $image->hash = str_random(30);
                $image->filename = $filename;
                $image->item_id = $item->id;
                $image->save();

                $uploadCount ++;
            }
        }

        if($uploadCount == $file_count)
        {
            Session::flash('success', 'Item created.');
            return Redirect::to('admin/dashboard');
        }else{
            return Redirect::to('admin/dashboard')->withInput()->withErrors($validator);
        }
    }
twg_'s avatar
Level 6

@bobbybouwmann I took your advice and subscribed to laracast. I watched the video for dropzone and have it all working.

I'm still trying to figure out a bit of logic though.

  1. I would like refresh my page after the upload(s) complete to show the thumbnails of the uploaded images on the page.
  2. I would like to limit the number of images to a post to 3. I'm trying to figure out how I can allow only 3 images to be uploaded and then disable the dropzone box so no more images can be uploaded.
bobbybouwmann's avatar
  1. If you want to refresh the page after you need to fetch the images from where you put them and display it to the page. I believe one of the videos is already showing how to fetch the images on a next page, so you basically do the same thing here!

  2. It's all in the documentation: http://www.dropzonejs.com/#config-maxFiles

twg_'s avatar
Level 6

I saw the documentation for limiting how many images could be uploaded each time but I need to limit it only 3 images being uploaded total to each item.

Doing it the way you send me in dropzones docs would allow a user to "edit" the page and add up to 3 images each time.

Please or to participate in this conversation.