/** * Store a newly created resource in storage. * * @return Response */ public function store() { // getting all of the post data $file = array('image' => Input::file('image'), 'name' => Input::get('name'), 'link' => Input::get('link'), 'max_cashback' => Input::get('max_cashback'), 'description' => Input::get('description'), 'featured' => Input::get('featured')); $name = Input::get('name'); $file['slug'] = str_replace(' ', '-', strtolower($name)); $store = new Store($file); $rules = ['image' => 'required', 'max_cashback' => 'required', "name" => "required", 'description' => 'required', 'slug' => 'required|unique:stores|max:255', "link" => "required"]; // doing the validation, passing post data, rules and the messages $validator = Validator::make($file, $rules); if ($validator->fails()) { // send back to the page with the input data and errors return Redirect::to('admin/stores/create')->with('store', $store)->withErrors($validator); } else { // checking file is valid. if (Input::file('image')->isValid()) { $destinationPath = 'stores'; // upload path $extension = Input::file('image')->getClientOriginalExtension(); // getting image extension $fileName = str_replace(' ', '_', $file['name']) . '_' . rand(111111, 999999) . '.' . $extension; // renameing image Input::file('image')->move($destinationPath, $fileName); // uploading file to given path $file['image'] = $destinationPath . '/' . $fileName; if (!isset($file['featured'])) { $file['featured'] = ''; } $store = new Store($file); $store->save(); Session::flash('status', 'Store added successfully'); return redirect('admin/stores'); } else { // sending back with error message. // Session::flash('error', 'uploaded file is not valid'); return Redirect::to('admin/stores/create')->withErrors('error', 'uploaded file is not valid'); } } }