IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/DepartmentController.php

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class DepartmentController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $departments = \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idDepartment', 'desc')->get();
        return view('schools.hrms.department', compact('departments'));
    }

    /**
     * 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) {
        $rules = [];
        foreach ($request->departments as $key => $val) {
            $rules['departments.' . $key . '.departmentName'] = 'unique:departments,departmentName,NULL,idDepartment,idSchool,' . Auth::guard('school')->user()->idSchool;
        }
        $message = [
            'departments.*departmentName.unique' => 'Department  Already Exist.',
        ];
        $this->Validate($request, $rules, $message);
        foreach ($request->departments as $var) {
            $dept = new \App\Department();
            $dept->fill($request->all());
            $dept->idSchool = Auth::guard('school')->user()->idSchool;
            $dept->departmentName = $var['departmentName'];
            $dept->save();
        }
        flash('Departments has been added successfully!!');
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
        return redirect('school/departments');
    }

    /**
     * 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) {
        $departments = \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idDepartment', 'desc')->get();
        $department = \App\Department::where('idDepartment', '=', $id)->first();
        return view('schools.hrms.department', compact('departments','department'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        $dept = \App\Department::where('idDepartment', '=', $id)->first();
        $rules = [
            'departmentName' => 'required|unique:departments,departmentName,' . $id . ',idDepartment,idSchool,' . Auth::guard('school')->user()->idSchool,
        ];
        $this->Validate($request, $rules);
        $dept->fill($request->all());
        $dept->update();
        return redirect('school/departments');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $dept = \App\Department::where('idDepartment', '=', $id)->first();
        $dept->delete();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

    public function getDesignation($id) {
        if($id == "All") $designations = \App\Designation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('designationName','idDesignation')->toArray();
        else $designations = \App\Designation::where('idDepartment','=',$id)->get()->pluck('designationName','idDesignation')->toArray();
        return json_encode($designations);
    }
}

Copyright © 2021 - 2025 IMMREX7