Untitled
unknown
php
a year ago
2.1 kB
2
Indexable
Never
<?php namespace App\Http\Controllers; use App\Http\Requests\StoreBookRequest; use App\Http\Requests\UpdateBookRequest; use App\Jobs\BookPublishJob; use App\Models\Author; use App\Models\Book; use Illuminate\Contracts\Pagination\Paginator; class BookController extends Controller { /** * Display a listing of the resource. */ public function index() { /* @var Paginator */ $books = Book::paginate(3); return view("books.index", ["books" => $books]); } /** * Show the form for creating a new resource. */ public function create() { $authors = Author::with("user")->get(); return view("books.create", ["authors" => $authors]); } /** * Store a newly created resource in storage. */ public function store(StoreBookRequest $request) { /* @var Book */ $book = Book::create($request->all()); if ($request->has("authors")) { $book->authors()->attach($request->get("authors")); } BookPublishJob::dispatch($book); return redirect() ->action([BookController::class, 'show'], ['book' => $book->id]); } /** * Display the specified resource. */ public function show(Book $book) { return view("books.show", ["book" => $book]); } /** * Show the form for editing the specified resource. */ public function edit(Book $book) { $authors = Author::with("user")->get(); return view("books.edit", compact("authors", "book")); } /** * Update the specified resource in storage. */ public function update(UpdateBookRequest $request, Book $book) { $book->fill($request->all()); $book->save(); if ($request->has("authors")) { $book->authors()->sync($request->get("authors")); } return view("books.show", ["book" => $book]); } /** * Remove the specified resource from storage. */ public function destroy(Book $book) { $book->delete(); return redirect("/books"); } }
Leave a Comment