/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $user_id = Auth::user()->id; $data = INPUT::all(); $rules = $this->rules(); $messages = $this->messages(); $validator = Validator::make($data, $rules, $messages); if (!$validator->fails()) { $book = new Book(); $book->id_user = $user_id; $book->title = $data['title']; $book->subtitle = $data['subtitle']; $book->publishedDate = $data['publishedDate']; $book->description = $data['description']; $book->pages = $data['pages']; $book->isbn10 = $data['isbn10']; $book->isbn13 = $data['isbn13']; $book->price_day = $data['price_day']; $book->price_bail = $data['price_bail']; $book->price_sale = $data['price_sale']; $book->language = $data['language']; if ($data['publisher'] != '0') { $book->id_publisher = $data['publisher']; } else { $publisher = new Publisher(); $publisher->publisher = $data['newpublisher']; $publisher->save(); $book->id_publisher = $publisher->id; } $book->cover = file_get_contents($data['cover']); $book->save(); $book_id = $book->id; $book_collection = new Book_Collection(); $book_collection->book_id = $book_id; $book_collection->collection_id = $data['collection']; $authors = explode(',', $data['authors']); foreach ($authors as $author) { $tmp = Author::where('name', 'like', $author)->limit(1)->get(); if (count($tmp) > 0) { $author_book = new Author_Book(); $author_book->book_id = $book_id; $author_book->author_id = $tmp[0]->id; $author_book->save(); } else { $newAuthor = new Author(); $newAuthor->name = $author; $newAuthor->save(); $author_id = $newAuthor->id; $author_book = new Author_Book(); $author_book->book_id = $book_id; $author_book->author_id = $author_id; $author_book->save(); } } return redirect('book/show/' . $book_id . ''); } else { return back()->withInput($data)->withErrors($validator); } }
function getExample4() { //CREATE $book = new Book(); $book->title = 'Harry Potter'; $book->author = 'J.K.Rowling'; $book->save(); return 'example 4'; }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(BookRegistrationRequest $request) { $book = new Book(); $book->title = $request->title; $book->author = $request->author; $book->language = $request->language; $book->publication_date = $request->publication_date; $book->save(); return new JsonResponse(['msg' => 'OK', 'id' => $book->id], 201); }
/** * * Tests the module showing details of a single book is working by first adding a new book * and matching the data inserted vs data seen. * */ public function testProcessBookShow() { //create a book $book = new Book(); $book->title = 'The Hobbit'; $book->author = 'Tolkien'; $book->save(); //see if the created book shows the same details used earlier $bookShowURL = '/books/' . $book->id; $this->visit($bookShowURL)->see('Tolkien')->see('The Hobbit'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $book = new Book(); $book->name = $request->name; $book->product_code = $request->product_code; $book->kStatus = $request->kStatus; $book->authors = $request->authors; $book->description = $request->description; $book->price = $request->price; $book->user_id = Auth::user()->id; $book->save(); return redirect()->route('book.index'); }
public function run() { DB::table('books')->truncate(); for ($i = 0; $i < 10; $i++) { $book = new Book(); $book->book_name = str_random(10); $book->book_auther = str_random(10); $book->book_press = str_random(10); $book->book_num = 10; $book->book_res = 10; $book->save(); } }
public function store(Request $request) { $this->validate($request, ['bk_isbn' => 'required|max:225', 'bk_title' => 'required|max:225', 'bk_author' => 'required|max:225', 'bk_publisher' => 'required|max:225', 'bk_count' => 'required|integer']); $books = new Book(); $books->bk_isbn = $request->bk_isbn; $books->bk_title = $request->bk_title; $books->bk_author = $request->bk_author; $books->bk_publisher = $request->bk_publisher; $books->bk_count = $request->bk_count; $books->bk_desc = $request->bk_desc; $books->save(); return redirect('books'); }
public function postAddbook(Request $request) { $this->validate($request, ['bookname' => 'required|max:200', 'author' => 'max:50', 'count' => 'required|integer', 'introduction' => 'string']); $book = new Book(); $book->bookname = $request->input('bookname'); $book->count = $request->input('count'); $book->author = $request->input('author'); $book->introduction = $request->input('introduction'); $book->user_id = Auth::user()->id; if (!$book->save()) { return Redirect::back()->withErrors('数据无法被存储,请重新提交!'); } return Redirect::back()->withErrors('数据提交成功!'); }
/** * 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(); } }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, ['name' => 'required']); $book = new Book(); $book->shortname = $request->shortname; $book->name = $request->name; $book->pack = $request->pack; $book->bookgroup_id = $request->bookgroup_id ?: null; $book->book_type = $request->book_type; $book->price_buy = $request->price_buy; $book->price = $request->price; $book->price_shop = $request->price_shop; $book->user_id = Auth::user()->id; $book->save(); return redirect()->route('books.index'); }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $rules = array('title' => 'required', 'author' => 'required|alpha', 'year' => 'required|numeric', 'genre' => 'required|alpha'); $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { return Response::json($validator->messages(), 500); } else { $book = new Book(); $book->title = $request->title; $book->author = $request->author; $book->year = $request->year; $book->genre = $request->genre; $book->save(); Session::flash('message', 'New book successfully created'); return Response::json($book); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $data = array('errNum' => 0, 'errMsg' => '', 'errDate' => ''); $book = new Book(); $book->book_name = $request->book_name; $book->book_auther = $request->book_auther ? $request->book_auther : ""; $book->book_press = $request->book_press ? $request->book_press : ""; $book->book_num = $request->book_num; $book->book_res = $request->book_res; if ($book->save()) { $data['errMsg'] = "添加成功"; return json_encode($data); } else { $data['errMsg'] = "添加失败"; return json_encode($data); } }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $rules = array('title' => 'required', 'author' => 'required|regex:/^([A-Za-z]+)$/', 'year' => 'required|numeric', 'genre' => 'required|regex:/^([A-Za-z]+)$/'); $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { return Redirect::to('book/create')->withErrors($validator)->withInput(); } else { $book = new Book(); $book->title = $request->title; $book->author = $request->author; $book->year = $request->year; $book->genre = $request->genre; $book->save(); Session::flash('message', 'Successfully created book'); return Redirect::to('book'); } }
public function saveBook(Request $request) { if ($request->book_id != "") { $book = Book::find($request->book_id); $book->title = $request->title; $book->genre_id = $request->genre_id; $book->author_id = $request->author_id; $book->publisher_id = $request->publisher_id; $book->image = $request->image; $book->isbn = $request->isbn; $book->description_short = $request->description_short; $book->description = $request->description; $book->price = $request->price; $book->sale = $request->sale; $book->quantity = $request->quantity; $check = $book->save(); if ($check) { return "EDIT_SUCCEED"; } else { return "Có lỗi xảy ra. Vui lòng thử lại sau!"; } } else { $book = new Book(); $book->title = $request->title; $book->genre_id = $request->genre_id; $book->author_id = $request->author_id; $book->publisher_id = $request->publisher_id; $book->image = $request->image; $book->isbn = $request->isbn; $book->description_short = $request->description_short; $book->description = $request->description; $book->price = $request->price; $book->sale = $request->sale; $book->quantity = $request->quantity; $check = $book->save(); if ($check) { $genre_name = Genre::find($request->genre_id)->name; $author_name = Author::find($request->author_id)->name; $data = array('msg' => 'ADD_SUCCEED', 'book_id' => $book->id, 'genre_name' => $genre_name, 'author_name' => $author_name); return $data; } else { return "Có lỗi xảy ra. Vui lòng thử lại sau!"; } } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $rules = array('title' => 'required', 'author' => 'required|alpha', 'year' => 'required|numeric', 'genre' => 'required|alpha'); $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { return Redirect::to('books/create')->withErrors($validator)->withInput(); } else { $book = new Book(); $book->title = $request->title; $book->author = $request->author; $book->year = $request->year; $book->genre = $request->genre; $book->save(); Session::flash('message', 'New book successfully created'); $job = new Jobs\SendAddNewBookEmail($book); $this->dispatch($job); return Redirect::to('books'); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { // $this->validate($request, ['book_name' => 'required', 'author' => 'required', 'ISBN' => 'required', 'press_name' => 'required', 'press_date' => 'required', 'url' => 'required', 'in_use' => 'required']); $book = new Book(); $book->book_name = Input::get('book_name'); $book->author = Input::get('author'); $book->ISBN = Input::get('ISBN'); $book->press_name = Input::get('press_name'); $book->press_date = Input::get('press_date'); $book->url = Input::get('url'); $book->in_use = Input::get('in_use'); // $book ->id = 1; if ($book->save()) { return Redirect::to('admin'); } else { return Redirect::back()->withInput()->withErrors('fail to save!'); } }
public function postCreate(BookRequest $request) { $book = new Book(); $book->user_id = Auth::id(); $book->title = $request->title; $book->cate_id = $request->category; $book->description = $request->description; $book->detailinfo = $request->detailinfo; $book->alias = str_slug($request->title, '-'); //str_slug method sames as convertToAlias method // if(empty($request->alias) || $request->alias='') { // //$book -> alias = $this->convertToAlias($request->title); // $book -> alias = str_slug($request->title,'-'); // } // else { // $book -> alias = $request->alias; // } $book->author = $request->author; $book->status = $request->status == "on" ? 1 : 0; $thumbnail = ""; if (Input::hasFile('thumbnail')) { $file = Input::file('thumbnail'); $filename = $file->getClientOriginalName(); $extension = $file->getClientOriginalExtension(); $thumbnail = $book->alias . time() . '.' . $extension; } if ($thumbnail != "") { $book->thumbnail = $thumbnail; } else { $book->thumbnail = 'default.jpg'; } $book->save(); $affectedRows = Book::where('id', '=', $book->id)->update(array('alias' => $book->alias . '-' . $book->id)); if (Input::hasFile('thumbnail')) { $destinationPath = public_path() . '/uploads/products/images/'; Input::file('thumbnail')->move($destinationPath, $thumbnail); } //\Flash::success('Thao tác thành công!'); \Session::flash('edit_message', 'Thêm thành công!'); return $this->getCreate(); // return view('book.create'); }
public function store(Request $request) { $user_id = Auth::user()->id; $rules = ['title' => 'required', 'school' => 'required|integer', 'program' => 'required|integer', 'course' => 'required|integer']; $input = Input::only('title', 'school', 'program', 'course'); $validator = Validator::make($input, $rules); if ($validator->fails()) { return Redirect::back()->withInput()->withErrors($validator); } $book = new Book(); $book->title = $request->input('title'); $book->isbn = $request->input('isbn'); $book->description = $request->input('description'); $book->category = $request->input('category'); $book->school_id = $request->input('school'); $book->program_id = $request->input('program'); $book->course_id = $request->input('course'); $book->user_id = $user_id; $book->save(); return redirect()->route('book_create'); }
public static function saveBook($data) { $book = new Book(); //TODO: validation for data $book->title = $data['title']; $book->isbn10 = $data['isbn10']; $book->cover_pic = $data['picture']; $author = new Author(); $genre = new Genre(); $author->name = $data['author']; $genre->name = $data['genre']; $existingAuthor = Author::find($data['existing-author']); $existingGenre = Genre::find($data['existing-genre']); $book->save(); if ($data['author'] != '') { $book->authors()->save($author); } if ($data['genre'] != '') { $book->genres()->save($genre); } $book->authors()->attach($existingAuthor['author_id']); $book->genres()->attach($existingGenre['genre_id']); }
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'); }
| It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('welcome'); }); Route::get('create_book', function () { $book = new Book(); $book->title = 'My Second Boook'; $book->pages_count = '200'; $book->price = 10.5; $book->description = 'A not very original story'; $book->author_id = 1; $book->publisher_id = 1; $book->save(); }); /** * Chain as many queries * Chained conditions are equal to COND1 AND COND2 * 'get' returns a collection of instances * 'first' returns a single instance */ Route::get('where', function () { dd(Book::where('pages_count', '=', 200)->Where('price', '=', 10.5)->get()); }); /** * Chain as many queries * Chained conditions are equal to COND1 AND COND2 */ Route::get('orWhere', function () {
public function addbooks() { header('Content-Type: text/html; charset=utf-8'); for ($i = 0; $i <= 10; $i++) { $user = new User(); $user->name = $this->generateRandomString(5); $user->password = bcrypt($user->name); $user->email = $user->name . '@gmail.com'; $user->save(); } $names = ['%22feliz%20gouveia%22', '%22nuno%20ribeiro%22', '%22jose%20torres%22', '%22borges%20gouveia%22', '%22Sophia%20breyner%22', '%22Fernando%20Pessoa%22', '%22Eça%20De%20Queiros%22']; foreach ($names as $name) { $content = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=' . $name); $content = json_decode($content, true); $auts = Author::all('name'); $authors = array(); foreach ($auts as $aut) { $authors[] = $aut['name']; } $pubs = Publisher::all('publisher'); $publishers = array(); foreach ($pubs as $pub) { $publishers[] = $pub['publisher']; } $cats = Collection::all('collection'); $categories = array(); foreach ($cats as $cat) { $categories[] = $cat['collection']; } foreach ($content['items'] as $data) { $data = $data['volumeInfo']; $book = new Book(); if (isset($data['title'])) { $book->title = $data['title']; } if (isset($data['subtitle'])) { $book->subtitle = $data['subtitle']; } if (isset($data['publishedDate'])) { $book->publishedDate = $data['publishedDate']; } if (isset($data['description'])) { $book->description = $data['description']; } if (isset($data['pageCount'])) { $book->pages = $data['pageCount']; } if (isset($data['language'])) { $book->language = $data['language']; } if (isset($data['industryIdentifiers'])) { foreach ($data['industryIdentifiers'] as $value) { if ($value['type'] == 'ISBN_13') { $book->isbn13 = $value['identifier']; } if ($value['type'] == 'ISBN_10') { $book->isbn10 = $value['identifier']; } } } if (isset($data['publisher'])) { if (!in_array($data['publisher'], $publishers)) { $publishers[] = $data['publisher']; $publisher = new Publisher(); $publisher->publisher = $data['publisher']; $publisher->save(); $publisher_id = $publisher->id; } else { $publisher = Publisher::where('publisher', '=', $data['publisher'])->limit(1)->get(); $publisher_id = $publisher[0]->id; } } if (isset($data['imageLinks']['thumbnail'])) { $book->cover = file_get_contents($data['imageLinks']['thumbnail']); } $book->price_day = random_int(0, 15); $book->price_bail = random_int(10, 50); $book->price_sale = random_int(10, 79); $books[] = $book; $book->id_publisher = $publisher_id; $book->id_user = rand(1, 10); $book->save(); $book_id = $book->id; if (isset($data['categories'])) { foreach ($data['categories'] as $value) { if (!in_array($value, $categories)) { $categories[] = $value; $category = new Collection(); $category->collection = $value; $category->save(); $category_id = $category->id; $collections = new Book_Collection(); $collections->book_id = $book_id; $collections->collection_id = $category_id; $collections->save(); } else { $category = Collection::where('collection', '=', $value)->limit(1)->get(); $collections = new Book_Collection(); $collections->book_id = $book_id; $collections->collection_id = $category[0]->id; $collections->save(); } } } if (isset($data['authors'])) { foreach ($data['authors'] as $value) { if (!in_array($value, $authors)) { $authors[] = $value; $author = new Author(); $author->name = $value; $author->save(); $author_id = $author->id; $author_book = new Author_Book(); $author_book->book_id = $book_id; $author_book->author_id = $author_id; $author_book->save(); } else { $author = Author::where('name', 'like', $value)->limit(1)->get(); $author_book = new Author_Book(); $author_book->book_id = $book_id; $author_book->author_id = $author[0]->id; $author_book->save(); } } } # code... } } }
/** * * After validation bulk book data of array containing multiple entities of Book * are passed as argument to be saved in the db in bulk * * @param $books */ public function storeBooksInBulk($books) { foreach ($books as $book) { $storeBook = new Book(); $storeBook->title = $book['title']; $storeBook->author = $book['author']; $storeBook->save(); unset($storeBook); } }
/** * Update a book. * * @param int $id * @return Response */ public function update(Request $request, Book $book) { $this->validate($request, ['isbn' => 'required', 'title' => 'required', 'author' => 'required', 'publisher' => 'required', 'description' => 'required']); $book->isbn = $request->isbn; $book->title = $request->title; $book->author = $request->author; $book->publisher = $request->publisher; if ($request->file('image') != null) { $book->image = $book->id . '.' . $request->file('image')->getClientOriginalExtension(); $request->file('image')->move(base_path() . '/resources/img/', $book->image); } $book->description = $request->description; $book->save(); return redirect('/books'); }
public function bookInAction(Request $request) { $name = $request->input("name"); $author = $request->input("author"); $publishing = $request->input("publishing"); $price = $request->input("price"); $quantity_in = $request->input("quantity-in"); $category = $request->input("category"); $isbn = $request->input("isbn"); if ($name == "" || $author == "" || $publishing == "" || $price == "" || $quantity_in == "" || $category == "" || $isbn == "") { return redirect('/admin/bookIn')->with("msg", "参数错误,请检查!"); } $book = new Book(); $book['book-name'] = $name; $book['author'] = $author; $book['publishing'] = $publishing; $book['category-id'] = intval($category); $book['price'] = $price; $book['date-in'] = date("Y-m-d"); $book['quantity-in'] = $quantity_in; $book['isbn'] = $isbn; $book->save(); //处理成功就返回添加页面 return redirect('/admin/bookIn')->with("msg", "入库成功"); }
function getExample7() { /*$book = new Book(); $results = $book->where('published', '>', 1950)->get(); foreach($results as $result) { echo $result->title.'<br/>'; } echo 'Number found is '.count($results).'<br/>'; return 'example7';*/ $author = new \App\Author(); $author->first_name = 'J.K'; $author->last_name = 'Rowling'; $author->bio_url = 'https://en.wikipedia.org/wiki/J._K._Rowling'; $author->birth_year = '1965'; $author->save(); dump($author->toArray()); $book = new \App\Book(); $book->title = "Harry Potter and the Philosopher's Stone"; $book->published = 1997; $book->cover = 'http://prodimage.images-bn.com/pimages/9781582348254_p0_v1_s118x184.jpg'; $book->purchase_link = 'http://www.barnesandnoble.com/w/harrius-potter-et-philosophi-lapis-j-k-rowling/1102662272?ean=9781582348254'; $book->author()->associate($author); # <--- Associate the author with this book $book->save(); dump($book->toArray()); return 'Example 7'; }