middleware([ "auth" ]); $this->middleware([ "lang" ]); $this->middleware([ "check.auth:washing.machine.show" ])->only("show", "index"); $this->middleware([ "check.auth:washing.machine.create" ])->only("create", "store"); $this->middleware([ "check.auth:washing.machine.edit" ])->only("edit", "update"); $this->middleware([ "check.auth:washing.machine.delete" ])->only("delete"); } /** * Display a listing of the resource. * * @param Request $request * @return Application|Factory|View */ public function index(Request $request) { $machines = WashingMachine::query()->get(); return Response::detect("washing-machines.index", [ "machines" => $machines ]); } /** * Show the form for creating a new resource. * * @return Application|Factory|View */ public function create() { $locations = Location::all(); return Response::detect("washing-machines.create", ["locations" => $locations] ); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return Application|Factory|View */ public function store(Request $request) { $data = $request->validate([ "name" => "required", "location_id" => "required" ]); $machine = new WashingMachine($data); $allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->get(); // If there already is a washing machine with that name, then don't add it if (count($allMachines) > 0) return redirect()->route("washing-machines.store"); else { // Else - Add it $machine->save(); $machines = WashingMachine::query()->get(); return redirect()->route("washing-machines.index", ['machines' => $machines]); } } /** * Display the specified resource. * * @param int $id * @return Application|Factory|View */ public function show($id) { } /** * Show the form for editing the specified resource. * * @param int $id * @return Application|Factory|View */ public function edit($id) { $machine = WashingMachine::find($id); $locations = Location::all(); return Response::detect("washing-machines.edit", [ "machine" => $machine, "locations" => $locations ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return Application|Factory|View */ public function update(Request $request, $id) { $data = $request->validate([ "name" => "required", "location_id" => "required", ]); $machine = WashingMachine::find($id); $allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->where('id', '!=', $id)->get(); // If there already is a washing machine with that name, then don't change it if (count($allMachines) > 0) return redirect()->route("washing-machines.store"); else { // Else - Change the name $machine->update($data); $machine->save(); $machines = WashingMachine::query()->get(); return redirect()->route("washing-machines.index", ["machines" => $machines]); } } /** * Remove the specified resource from storage. * * @param $id * @return Response */ public function destroy($id) { $machine = WashingMachine::find($id); $allReservations = WashingReservation::query()->where('machine_id', '=', $id); $allReservations->delete(); $machine->delete(); return redirect()->route("washing-machines.index"); } public function search(Request $request) { if ($request->ajax()) { $output = "" . "Navn" . "Lokation" . "\"Update\"" . "\"Delete\"" . ""; if ($request->option !== 'all') { $machines = DB::table('washing_machines') ->join('locations', 'washing_machines.location_id', '=', 'locations.id') ->select(DB::raw('washing_machines.name as washing_name, locations.name as location_name, washing_machines.id as id')) ->where('locations.id', '=', $request->option) ->get(); if (count($machines) !== 0) { foreach ($machines as $machine) { $output .= '' . '' . $machine->washing_name . '' . '' . $machine->location_name . '' . ' $machine->id]) . '">Update' . '
$machine->id]) . '" class="w-100 nostyle">' . csrf_field() . method_field("delete") . '' . '
' . '' . ''; } } else { $output .= '' . 'Det er ikke blivet oprettet nogen vaskmaskine på denne lokation endnu' . '' . '' . '' . ''; } } elseif ($request->option === "all") { $machines = WashingMachine::all(); if (count($machines) !== 0) { foreach ($machines as $machine) { $output .= '' . '' . $machine->name . '' . '' . Location::query()->where("id", "=", $machine->location_id)->first()->name . '' . ' $machine->id]) . '">Update' . '
' . csrf_field() . method_field("delete") . '' . '
' . '' . ''; } } else { $output .= '' . 'Det er ikke blivet oprettet nogen vaskmaskine på denne lokation endnu' . '' . '' . '' . ''; } } return Response($output); } } //Used for checking if the currently typed washingmachine name is unique. Create version public function nameCheck(Request $request){ $washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->get(); if(count($washing) > 0 && $request->nameCheck !== NULL){ return 1; } } //Used for checking if the currently typed washingmachine name is unique. Edit version public function nameCheckUpdate(Request $request){ $washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->where('id', '!=', $request->id)->get(); if(count($washing) > 0 && $request->nameCheck !== NULL){ return 1; } } }