public function store(Request $request) { $communityId = $request->get('community_id'); $galleryInstance = Gallery\GalleryInstance::where('community_id', '=', $communityId)->where('gallery_id', '=', 1)->first(); if (is_null($galleryInstance)) { $galleryInstance = new Gallery\GalleryInstance(); $galleryInstance->gallery_id = 1; $galleryInstance->community_id = $communityId; $galleryInstance->save(); } //create upload dir $fileArr = explode('.', $_FILES['image']['name']); $fileName = str_replace(' ', '', $fileArr[0]) . uniqid() . '.' . $fileArr[count($fileArr) - 1]; $uploadDir = 'uploads/galleryInstances/' . $galleryInstance->id; if (!file_exists($uploadDir)) { mkdir($uploadDir); } // Database record $galleryItem = new Gallery\GalleryItem(); $galleryItem->gallery_instance_id = $galleryInstance->id; $galleryItem->filename = $fileName; if ($galleryItem->save()) { move_uploaded_file($_FILES['image']['tmp_name'], $uploadDir . '/' . $fileName); } return $galleryInstance->items; }