123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Category;
- use Illuminate\Http\Request;
- class CategoryController extends Controller {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index() {
- $categories = Category::paginate(10);
- return view('categories.index', compact('categories'));
- }
- /**
- * Display a search listing of the resource.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function search(Request $request) {
- $oldSearch = $request->search;
- $categories = Category::where('name', 'like', '%' . $request->search . '%')
- ->paginate(10);
-
- return view('categories.index', compact('oldSearch', 'categories'));
- }
- /**
- * Display a search listing of content of the resource.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Models\Category $category
- * @return \Illuminate\Http\Response
- */
- public function searchBooks(Request $request, Category $category) {
- $oldSearch = $request->search;
- $books = $category->books()
- ->where('title', 'like', '%' . $request->search . '%')
- ->orWhere('authors', 'like', '%' . $request->search . '%')
- ->orWhere('editorial', 'like', '%' . $request->search . '%')
- ->paginate(6);
- return view('categories.show', compact('category', 'oldSearch', 'books'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create() {
- return view('categories.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request) {
- $input = $request->validate([
- 'name' => 'required|string|max:255|unique:categories,name',
- 'description' => 'nullable|string|max:1024'
- ]);
- Category::create($input);
- return redirect()->route('categories.index')
- ->with('success', 'Category added successfully');
- }
- /**
- * Display the specified resource.
- *
- * @param \App\Models\Category $category
- * @return \Illuminate\Http\Response
- */
- public function show(Category $category) {
- $books = $category->books()->paginate(6);
- return view('categories.show', compact('category', 'books'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Models\Category $category
- * @return \Illuminate\Http\Response
- */
- public function edit(Category $category) {
- return view('categories.edit', compact('category'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Models\Category $category
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, Category $category) {
- $input = $request->validate([
- 'name' => 'required|string|max:255|unique:categories,name,' . $category->id,
- 'description' => 'nullable|string|max:1024'
- ]);
- $category->update($input);
- return redirect()->route('categories.index')
- ->with('success', 'Category modified successfully');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Models\Category $category
- * @return \Illuminate\Http\Response
- */
- public function destroy(Category $category) {
- $category->delete();
- return redirect()->route('categories.index')
- ->with('success', 'Category removed successfully');
- }
- }
|