123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\State;
- use App\Models\Country;
- class StateController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- $sort_country = $request->sort_country;
- $sort_state = $request->sort_state;
- $state_queries = State::query();
- if ($request->sort_state) {
- $state_queries->where('name', 'like', "%$sort_state%");
- }
- if ($request->sort_country) {
- $state_queries->where('country_id', $request->sort_country);
- }
- $states = $state_queries->orderBy('status', 'desc')->paginate(15);
- return view('backend.setup_configurations.states.index', compact('states', 'sort_country', 'sort_state'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $state = new State;
- $state->name = $request->name;
- $state->country_id = $request->country_id;
- $state->save();
- flash(translate('State has been inserted successfully'))->success();
- return back();
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $state = State::findOrFail($id);
- $countries = Country::where('status', 1)->get();
- return view('backend.setup_configurations.states.edit', compact('countries', 'state'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $state = State::findOrFail($id);
- $state->name = $request->name;
- $state->country_id = $request->country_id;
- $state->save();
- flash(translate('State has been updated successfully'))->success();
- return back();
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- State::destroy($id);
- flash(translate('State has been deleted successfully'))->success();
- return redirect()->route('states.index');
- }
- public function updateStatus(Request $request)
- {
- $state = State::findOrFail($request->id);
- $state->status = $request->status;
- $state->save();
- if ($state->status) {
- foreach ($state->cities as $city) {
- $city->status = 1;
- $city->save();
- }
- }
- return 1;
- }
- }
|