コード例 #1
0
 public function rateStore()
 {
     $rate;
     $title = Input::get("title");
     $update_day = Input::get("update_day");
     $content = Input::get("content");
     $top_content1 = Input::get("top_content1");
     $top_content2 = Input::get("top_content2");
     $top_content3 = Input::get("top_content3");
     $insert_array = array("title" => $title, "image" => Input::file('image'), "content" => $content);
     $rules = ['title' => 'required', 'content' => 'required'];
     $validator = Validator::make($insert_array, $rules);
     $messages = $validator->messages();
     if ($validator->fails()) {
         return Redirect::to('/rates/create')->withErrors($messages);
     } else {
         if (Input::hasFile('image')) {
             $file = Input::file('image');
             $size = $file->getSize();
             $file_max = 2048;
             $size = $size / 1024;
             if ($size > $file_max) {
                 return "檔案上傳限制只有2MB." . '<button onclick="goBack()">回到上一頁</button>' . "<script>\n   function goBack() {\n   window.history.back();\n   }\n   </script>";
             }
             $destinationPath = public_path() . '/img';
             $filename = str_random(6) . '_' . $file->getClientOriginalName();
             $uploadSuccess = $file->move($destinationPath, $filename);
             $rate = Rate::create(array('title' => $title, 'image' => '/img/' . $filename, 'content' => $content, 'update_day' => $update_day, "top_content1" => $top_content1, "top_content2" => $top_content2, "top_content3" => $top_content3, 'secret' => ''));
         } else {
             $rate = Rate::create(array('title' => $title, 'image' => '', 'update_day' => $update_day, 'content' => $content, "top_content1" => $top_content1, "top_content2" => $top_content2, "top_content3" => $top_content3, 'secret' => ''));
         }
         return Redirect::to('rates')->withInput()->with('success', '新增成功');
     }
 }