first commit
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class BookController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$books = Book::with('category')->get();
|
||||
return view('books.index', compact('books'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = Category::all();
|
||||
return view('books.create', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'author' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'publication_date' => 'nullable|date',
|
||||
'cover_image' => 'nullable|string|max:255',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
Book::create([
|
||||
'title' => $request->title,
|
||||
'slug' => Str::slug($request->title),
|
||||
'author' => $request->author,
|
||||
'description' => $request->description,
|
||||
'publication_date' => $request->publication_date,
|
||||
'cover_image' => $request->cover_image,
|
||||
'category_id' => $request->category_id,
|
||||
]);
|
||||
|
||||
return redirect()->route('books.index')
|
||||
->with('success', 'Book created successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Book $book)
|
||||
{
|
||||
return view('books.show', compact('book'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Book $book)
|
||||
{
|
||||
$categories = Category::all();
|
||||
return view('books.edit', compact('book', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Book $book)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'author' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'publication_date' => 'nullable|date',
|
||||
'cover_image' => 'nullable|string|max:255',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
$book->update([
|
||||
'title' => $request->title,
|
||||
'slug' => Str::slug($request->title),
|
||||
'author' => $request->author,
|
||||
'description' => $request->description,
|
||||
'publication_date' => $request->publication_date,
|
||||
'cover_image' => $request->cover_image,
|
||||
'category_id' => $request->category_id,
|
||||
]);
|
||||
|
||||
return redirect()->route('books.index')
|
||||
->with('success', 'Book updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Book $book)
|
||||
{
|
||||
$book->delete();
|
||||
|
||||
return redirect()->route('books.index')
|
||||
->with('success', 'Book deleted successfully.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user