Anons79 Mini Shell

Directory : /home/aplikasiposinfo/.trash/app.3/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/.trash/app.3/Http/Controllers/IndikatorPemerintahController.php

<?php

namespace App\Http\Controllers;

use App\Models\IndikatorPemerintah;
use App\Models\IndukOpd;
use App\Models\SasaranRpjmd;
use App\Models\visiRpjmd;

use Illuminate\Http\Request;

class IndikatorPemerintahController extends Controller
{
    protected $routeName = 'indikator_pemerintah';
    protected $viewName = 'indikator_pemerintah';
    protected $title = 'Indikator Pemerintah';

    public function index()
    {
        //
        $route = $this->routeName;
        $title = $this->title;
        $tahun = visiRpjmd::get();
        
        $tahunArray = [];
        
        foreach ($tahun as $item) {
            $range = range($item->tahun_awal, $item->tahun_akhir);
            $tahunArray = array_merge($tahunArray, $range);
        }

        // Hilangkan duplikat (karena 2025-2026 muncul dua kali)
        $tahunArray = array_unique($tahunArray);
        sort($tahunArray); // Urutkan lagi
        
        
        $tahun = $_GET['tahun'] ?? date('Y');
        
        $sasaranRpjmd = SasaranRpjmd::whereHas('Tujuan', function ($query) use ($tahun) {
            $query->whereHas('Misi', function ($q1)  use ($tahun) {
                $q1->whereHas('visi', function ($q2)  use ($tahun) {
                    $q2->where('tahun_awal', '<=', $tahun)->where('tahun_akhir', '>=', $tahun);
                });
            });
        })->with(['IndikatorPemerintah' => function ($q) use ($tahun) {
            $q->where('tahun', $tahun); // filter relasi berdasarkan tahun
        }])->get();

            //$sasaranRpjmd = SasaranRpjmd::whereHas('IndikatorPemerintah', function ($query) {
            //     $query->where('tahun', $_GET['tahun']);
            // })->get();
            
        $induk_opd_arr = IndukOpd::pluck('nama', 'id');
        return view('indikator_pemerintah.index',compact('route','title', 'sasaranRpjmd', 'induk_opd_arr', 'tahunArray'));

    }

    public function ApiIP($id, $tahun) {
        $indikatorPemerintah = IndikatorPemerintah::with('IndukOpd')->where('sasaran_rpjmd_id', $id)->where('tahun', $tahun)->get();


        return response()->json([
            'status' => 'success',
            'data' => $indikatorPemerintah,
            'id' => $id,
        ]);
    }

    /**
     * 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)
    {
        //
        try {
            IndikatorPemerintah::create($request->all());

            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil Menambah Data Indikator Pemerintah']);
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $indikatorPemerintah = IndikatorPemerintah::where('id', $id)->first();
        try {
            //code...
            $indikatorPemerintah->update($request->all());
            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil Mengubah Data Indikator Pemerintah '.$indikatorPemerintah->nama]);
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $indikatorPemerintah = IndikatorPemerintah::where('id', $id)->first();
        try {
            //code...
            $indikatorPemerintah->delete();

            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil Menghapus Data Indikator Pemerintah '.$indikatorPemerintah->nama]);
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }
    public function apiEdit($id){
        $indikatorPemerintah = IndikatorPemerintah::where('id', $id)->first();
        $indukOpd = IndukOpd::all();


        return response()->json([
            'status' => 'success',
            'data' => $indikatorPemerintah,
            'IndukOpd' => $indukOpd,
        ]);
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]