public function postAdd() { $rules = array('isbn' => 'required|size:13', 'title' => 'required|min:3', 'author' => 'required|alpha', 'category' => 'required', 'publishing_house' => 'required', 'page_no' => 'required|numeric', 'publishing_year' => 'required|max:4'); $validator = Validator::make(Input::all(), $rules); if ($validator->passes()) { //all fields are set $isbn = Input::get('isbn'); //check if isbn exists in the books table $_test = Book::where('isbn', '=', $isbn)->first(); if ($_test != null) { //the book is in the books table, just add him for this user $mybook = new LibraryBooks(); $mybook->user_id = Auth::user()->id; $mybook->book_isbn = $isbn; $mybook->copies_no = 1; //TODO: change this $mybook->save(); return "This book was saved on library_books"; } else { //the book doesn't exist in the books table //add it there then add it to the user $mybook = new Book(); $mybook->author = Input::get('author'); $mybook->category = Input::get('category'); $mybook->isbn = $isbn; $mybook->page_no = Input::get('page_no'); $mybook->publishing_house = Input::get('publishing_house'); $mybook->publishing_year = Input::get('publishing_year'); $mybook->title = Input::get('title'); $mybook->save(); //now that the book was added to books //add him to the user $new_book = new LibraryBooks(); $new_book->user_id = Auth::user()->id; $new_book->book_isbn = $isbn; $new_book->copies_no = 1; //TODO: change this $new_book->save(); return "This was stored to books with id " . $mybook->id; } } else { return Redirect::to('/api/v1/add')->withErrors($validator); } }
public function run() { $faker = Faker\Factory::create(); //getting all books from the table $books = Book::all(); $isbn_list = array(); //getting all ISBNs into a single array foreach ($books as $book) { array_push($isbn_list, $book->isbn); } DB::table('library_books')->delete(); for ($i = 0; $i < 15; $i++) { $fake_library_id = $faker->numberBetween(1, 10); $fake_book_isbn = $faker->randomElement($isbn_list); //checking for duplications if (LibraryBooks::where('user_id', '=', $fake_library_id)->where('book_isbn', '=', $fake_book_isbn)->count() > 0) { continue; } LibraryBooks::create(array('user_id' => $fake_library_id, 'book_isbn' => $fake_book_isbn, 'copies_no' => $faker->numerify('##'))); } }