function getExample4() { $book = new \App\Book(); $book->title = 'Harry Potter'; $book->author = 'J.k Rowling'; $book->save(); return 'Example 4'; }
public function postCreate(Request $request) { $this->validate($request, ['title' => 'required|min:5', 'author' => 'required|min:5', 'cover' => 'required|url', 'published' => 'required|min:4']); // Code here to enter book into the database $book = new \App\Book(); $book->title = $request->title; $book->author = $request->author; $book->cover = $request->cover; $book->published = $request->published; $book->purchase_link = $request->purchase_link; $book->save(); // Confirm book was entered: //return 'Process adding new book: '.$request->input('title'); //return view() \Session::flash('flash_message', 'Your book was added!'); return redirect('/books/create'); }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $title = $request->input('title'); $auth = $request->input('author'); $sum = $request->input('summary'); $rate = $request->input('rating'); #create a new book with data submitted $book = new \App\Book(); $book->title = $title; $book->author = $auth; $book->summary = $sum; $book->save(); #attach rating to book table $ratingBook = new \App\Rating(); $ratingBook->rating = $rate; $ratingBook->save(); $book->ratings()->attach($ratingBook); Session::flash('message', 'Successfully created book!'); $books = \App\Book::all(); return view('/books/member')->with('books', $books); }
/** * Responds to requests to POST /books/create */ public function postCreate(Request $request) { // validation $this->validate($request, ['title' => 'required|min:5', 'cover' => 'required|url', 'published' => 'required|min:4']); // Code here to enter into the database // new Book object // mass-assignment $book = new \App\Book(); $book->title = $request->title; //$book->author = $request->author; $book->author_id = $request->author; $book->user_id = \Auth::id(); $book->cover = $request->cover; $book->published = $request->published; $book->purchase_link = $request->purchase_link; $book->save(); // Confirm book was entered: //return 'Process adding new book' . $request->input('title'); \Session::flash('flash_message', 'Your book was added!'); return redirect('/books'); }
/** * Responds to requests to POST /books/create */ public function postCreate(Request $request) { $this->validate($request, ['title' => 'required|min:5', 'cover' => 'required|url', 'published' => 'required|min:4']); # Enter book into the database $book = new \App\Book(); $book->title = $request->title; $book->author_id = $request->author; $book->user_id = \Auth::id(); # <--- NEW LINE $book->cover = $request->cover; $book->published = $request->published; $book->purchase_link = $request->purchase_link; $book->save(); # Add the tags if ($request->tags) { $tags = $request->tags; } else { $tags = []; } $book->tags()->sync($tags); # Done \Session::flash('flash_message', 'Your book was added!'); return redirect('/books'); }
| */ Route::get('/', function () { return view('welcome'); }); Route::get('book_create', function () { echo "Creating the records...."; $title = 'Eze goes to College'; $pages_count = 800; $price = 15.5; $description = 'This is a very interesting book about Eze going to College and killing it'; $author_id = 1; $publisher_id = 1; // Method 1 of inserting records into the database via a Model - Mass Assignment $book = new \App\Book(['title' => $title, 'pages_count' => $pages_count, 'price' => $price, 'description' => $description, 'author_id' => $author_id, 'publisher_id' => $publisher_id]); $book->save(); // You have to call the save method here if using Method 1 technique // Use either of them to insert records into the database // For security issues it won't allow you create those records just like that, so... // Make sure you have properties $fillable or $guarded in your Model // e.g protected $fillable = ['title','pages_count','price','description','author_id','publisher_id'] // Method 2 of inserting records into the database via a Model - Mass Assignment $book = \App\Book::create(['title' => $title, 'price' => $price, 'pages_count' => $pages_count, 'description' => $description, 'author_id' => $author_id, 'publisher_id' => $publisher_id]); echo "Done...."; }); Route::get('book_get_all', function () { return \App\Book::all(); }); Route::get('book_get_2', function () { return \App\Book::findOrFail(2); });