StateController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\State;
  5. use App\Models\Country;
  6. class StateController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index(Request $request)
  14. {
  15. $sort_country = $request->sort_country;
  16. $sort_state = $request->sort_state;
  17. $state_queries = State::query();
  18. if ($request->sort_state) {
  19. $state_queries->where('name', 'like', "%$sort_state%");
  20. }
  21. if ($request->sort_country) {
  22. $state_queries->where('country_id', $request->sort_country);
  23. }
  24. $states = $state_queries->orderBy('status', 'desc')->paginate(15);
  25. return view('backend.setup_configurations.states.index', compact('states', 'sort_country', 'sort_state'));
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function create()
  33. {
  34. //
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param \Illuminate\Http\Request $request
  40. * @return \Illuminate\Http\Response
  41. */
  42. public function store(Request $request)
  43. {
  44. $state = new State;
  45. $state->name = $request->name;
  46. $state->country_id = $request->country_id;
  47. $state->save();
  48. flash(translate('State has been inserted successfully'))->success();
  49. return back();
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param int $id
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function show($id)
  58. {
  59. //
  60. }
  61. /**
  62. * Show the form for editing the specified resource.
  63. *
  64. * @param int $id
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function edit($id)
  68. {
  69. $state = State::findOrFail($id);
  70. $countries = Country::where('status', 1)->get();
  71. return view('backend.setup_configurations.states.edit', compact('countries', 'state'));
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @param int $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function update(Request $request, $id)
  81. {
  82. $state = State::findOrFail($id);
  83. $state->name = $request->name;
  84. $state->country_id = $request->country_id;
  85. $state->save();
  86. flash(translate('State has been updated successfully'))->success();
  87. return back();
  88. }
  89. /**
  90. * Remove the specified resource from storage.
  91. *
  92. * @param int $id
  93. * @return \Illuminate\Http\Response
  94. */
  95. public function destroy($id)
  96. {
  97. State::destroy($id);
  98. flash(translate('State has been deleted successfully'))->success();
  99. return redirect()->route('states.index');
  100. }
  101. public function updateStatus(Request $request)
  102. {
  103. $state = State::findOrFail($request->id);
  104. $state->status = $request->status;
  105. $state->save();
  106. if ($state->status) {
  107. foreach ($state->cities as $city) {
  108. $city->status = 1;
  109. $city->save();
  110. }
  111. }
  112. return 1;
  113. }
  114. }