Spaces:
Running
on
Zero
Running
on
Zero
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Models\Book; | |
class BookController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
$books = Book::all(); | |
return view('books.index', compact('books')); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() | |
{ | |
return view('books.create'); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$request->validate([ | |
'title' => 'required', | |
'author' => 'required', | |
'published_at' => 'required', | |
]); | |
Book::create($request->all()); | |
return redirect()->route('books.index'); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
$book = Book::find($id); | |
return view('books.show', compact('book')); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) | |
{ | |
$book = Book::find($id); | |
return view('books.edit', compact('book')); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$request->validate([ | |
'title' => 'required', | |
'author' => 'required', | |
'published_at' => 'required', | |
]); | |
$book = Book::find($id); | |
$book->update($request->all()); | |
return redirect()->route('books.index'); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
Book::destroy($id); | |
return redirect()->route('books.index'); | |
} | |
} |