TicketCategoryController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Controllers\Moderation;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Ticket;
  5. use App\Models\TicketCategory;
  6. use Illuminate\Http\Request;
  7. class TicketCategoryController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. return view('moderator.ticket.category');
  17. }
  18. /**
  19. * Store a newly created resource in storage.
  20. *
  21. * @param \Illuminate\Http\Request $request
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function store(Request $request)
  25. {
  26. $request->validate([
  27. 'name' => 'required|string|max:191',
  28. ]);
  29. TicketCategory::create($request->all());
  30. return redirect(route("moderator.ticket.category.index"))->with("success",__("Category created"));
  31. }
  32. /**
  33. * Show the form for editing the specified resource.
  34. *
  35. * @param int $id
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function edit($id)
  39. {
  40. //
  41. }
  42. /**
  43. * Update the specified resource in storage.
  44. *
  45. * @param \Illuminate\Http\Request $request
  46. * @param int $id
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function update(Request $request, $id)
  50. {
  51. //
  52. }
  53. /**
  54. * Remove the specified resource from storage.
  55. *
  56. * @param int $id
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function destroy($id)
  60. {
  61. $category = TicketCategory::where("id",$id)->firstOrFail();
  62. if($category->id == 5 ){ //cannot delete "other" category
  63. return back()->with("error","You cannot delete that category");
  64. }
  65. $tickets = Ticket::where("ticketcategory_id",$category->id)->get();
  66. foreach($tickets as $ticket){
  67. $ticket->ticketcategory_id = "5";
  68. $ticket->save();
  69. }
  70. $category->delete();
  71. return redirect()
  72. ->route('moderator.ticket.category.index')
  73. ->with('success', __('Category removed'));
  74. }
  75. public function datatable()
  76. {
  77. $query = TicketCategory::withCount("tickets");
  78. return datatables($query)
  79. ->addColumn('name', function ( TicketCategory $category) {
  80. return $category->name;
  81. })
  82. ->editColumn('tickets', function ( TicketCategory $category) {
  83. return $category->tickets_count;
  84. })
  85. ->addColumn('actions', function (TicketCategory $category) {
  86. return '
  87. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('moderator.ticket.category.destroy', $category->id).'">
  88. '.csrf_field().'
  89. '.method_field('DELETE').'
  90. <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  91. </form>
  92. ';
  93. })
  94. ->editColumn('created_at', function (TicketCategory $category) {
  95. return $category->created_at ? $category->created_at->diffForHumans() : '';
  96. })
  97. ->rawColumns(['actions'])
  98. ->make();
  99. }
  100. }