/** * Store a newly created resource in storage. * * @return Response */ public function store(BookRequest $request) { if (Input::file('cover')->isValid()) { $file = Input::file('cover'); $destinationPath = 'images/cover'; $fileName = rand(11111, 99999) . '_' . $file->getClientOriginalName(); Input::file('cover')->move($destinationPath, $fileName); $data = new Book(); $data->fill($request->all()); $data->cover = $fileName; $data->save(); return Redirect('admin/books')->with('successMessage', 'Berhasil menambah buku.'); } else { return Redirect()->back(); } }
public function importBooks() { $rules = array('csvfile' => 'required|mimes:csv,txt'); try { $file = Input::file('csvfile'); if (is_null($file)) { throw new Exception("File upload required"); } Excel::load($file, function ($reader) { $results = $reader->all(); foreach ($results as $row) { $book = new Book(); # First row should be like "Title, Author" $data = array('title' => $row->title, 'author' => $row->author); $book->fill($data); $book->save(); } }); } catch (Exception $e) { return Redirect::to('/')->with(['books' => Book::all(), 'errors' => $e->getMessage()]); } return Redirect::to('/')->with(['books' => Book::all()]); //return Redirect::to('/')->with('csvdata', $results); }
public function generate($vendor_slug, $tour_slug, $schedule_id) { // ------------------------------------------------- // check vendor // ------------------------------------------------- $vendor = Vendor::SlugIs($vendor_slug)->first(); if (!$vendor) { App::abort(404); } // ------------------------------------------------- // check tour // ------------------------------------------------- $tour = $vendor->tours()->slugIs($tour_slug)->first(); if (!$tour) { App::abort(404); } // ------------------------------------------------- // schedule // ------------------------------------------------- $schedule = $tour->schedules()->find($schedule_id); if (!$schedule) { App::abort(404); } // ------------------------------------------------- // generate voucher // ------------------------------------------------- $book = Book::where('user_id', '=', Auth::id())->ofSchedule($schedule->id)->first(); if (!$book) { $book = new Book(); $book->fill(['discount_currency' => $schedule->currency, 'discount' => $schedule->discount, 'discount_code' => str_random(6), 'user_id' => Auth::id(), 'schedule_id' => $schedule->id]); if (!$book->save()) { return redirect()->back()->withErrors($book->getErrors); } } return redirect()->route('web.me.index'); }