Jenderal92

Untitled

Jul 23rd, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1.  
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Category;
  5.  
  6. class CategoryController extends Controller
  7. {
  8. public function index()
  9. {
  10. $categories = Category::latest()->paginate(10);
  11. return view('categories.index',compact('categories'))
  12. ->with('i', (request()->input('page', 1) - 1) * 5);
  13. }
  14.  
  15. public function create()
  16. {
  17. return view('categories.create');
  18. }
  19.  
  20. public function store(Request $request)
  21. {
  22. request()->validate([
  23. 'category' => 'required'
  24. ]);
  25. $slug=str_slug($request->category);
  26. Category::create($request->all()+['slug'=>$slug]);
  27. return redirect()->route('categories.index')
  28. ->with('success','Category created successfully');
  29. }
  30.  
  31. public function edit(Category $category)
  32. {
  33. return view('categories.edit',compact('category'));
  34. }
  35.  
  36. public function update(Request $request,Category $category)
  37. {
  38. request()->validate([
  39. 'category' => 'required'
  40. ]);
  41. $slug=str_slug($request->category);
  42.  
  43. $category->update($request->all()+['slug'=>$slug]);
  44. return redirect()->route('categories.index')
  45. ->with('success','Category updated successfully');
  46. }
  47.  
  48. public function destroy($id)
  49. {
  50. Category::destroy($id);
  51. return redirect()->route('categories.index')
  52. ->with('success','Category deleted successfully');
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment