CategoryController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Category;
  4. use Illuminate\Http\Request;
  5. class CategoryController extends Controller {
  6. /**
  7. * Display a listing of the resource.
  8. *
  9. * @return \Illuminate\Http\Response
  10. */
  11. public function index() {
  12. $categories = Category::paginate(10);
  13. return view('categories.index', compact('categories'));
  14. }
  15. /**
  16. * Display a search listing of the resource.
  17. *
  18. * @param \Illuminate\Http\Request $request
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function search(Request $request) {
  22. $oldSearch = $request->search;
  23. $categories = Category::where('name', 'like', '%' . $request->search . '%')
  24. ->paginate(10);
  25. return view('categories.index', compact('oldSearch', 'categories'));
  26. }
  27. /**
  28. * Display a search listing of content of the resource.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @param \App\Models\Category $category
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function searchBooks(Request $request, Category $category) {
  35. $oldSearch = $request->search;
  36. $books = $category->books()
  37. ->where('title', 'like', '%' . $request->search . '%')
  38. ->orWhere('authors', 'like', '%' . $request->search . '%')
  39. ->orWhere('editorial', 'like', '%' . $request->search . '%')
  40. ->paginate(6);
  41. return view('categories.show', compact('category', 'oldSearch', 'books'));
  42. }
  43. /**
  44. * Show the form for creating a new resource.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function create() {
  49. return view('categories.create');
  50. }
  51. /**
  52. * Store a newly created resource in storage.
  53. *
  54. * @param \Illuminate\Http\Request $request
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function store(Request $request) {
  58. $input = $request->validate([
  59. 'name' => 'required|string|max:255|unique:categories,name',
  60. 'description' => 'nullable|string|max:1024'
  61. ]);
  62. Category::create($input);
  63. return redirect()->route('categories.index')
  64. ->with('success', 'Category added successfully');
  65. }
  66. /**
  67. * Display the specified resource.
  68. *
  69. * @param \App\Models\Category $category
  70. * @return \Illuminate\Http\Response
  71. */
  72. public function show(Category $category) {
  73. $books = $category->books()->paginate(6);
  74. return view('categories.show', compact('category', 'books'));
  75. }
  76. /**
  77. * Show the form for editing the specified resource.
  78. *
  79. * @param \App\Models\Category $category
  80. * @return \Illuminate\Http\Response
  81. */
  82. public function edit(Category $category) {
  83. return view('categories.edit', compact('category'));
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param \Illuminate\Http\Request $request
  89. * @param \App\Models\Category $category
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function update(Request $request, Category $category) {
  93. $input = $request->validate([
  94. 'name' => 'required|string|max:255|unique:categories,name,' . $category->id,
  95. 'description' => 'nullable|string|max:1024'
  96. ]);
  97. $category->update($input);
  98. return redirect()->route('categories.index')
  99. ->with('success', 'Category modified successfully');
  100. }
  101. /**
  102. * Remove the specified resource from storage.
  103. *
  104. * @param \App\Models\Category $category
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function destroy(Category $category) {
  108. $category->delete();
  109. return redirect()->route('categories.index')
  110. ->with('success', 'Category removed successfully');
  111. }
  112. }