Search code examples
laravellaravel-8laravel-validation

Laravel Custom Validation for Table


table 'angkatan'

I have table named 'angkatan' which has id, id_angkatan, nama_angkatan, and status. status column is enum contain 'aktif' and 'tidak aktif'. I want to give rules for this table in Laravel with validation where this table only allowed to have a maximum of 4 and a minimum of 1 of status 'aktif'. no rules for how many statuses 'tidak aktif'


$jml_angkatan_aktif = \App\Models\Angkatan::where('status', 'Aktif')->get('id_angkatan')->count();

With the $jml_angkatan_aktif variable I get the amount of id_angkatan which has 'Aktif' so I can count whether it is beyond 4 and under 1. but the problem is I can't give this custom validation. the purpose of this custom validation is I want people can't add 'angkatan' if there are already 4 status 'aktif' in this table. I hope you can understand my explanation. here is the code from AngkatanController in-store method.

public function store(Request $request) {
    $validator = Validator::make($request->all(),[
        'id_angkatan' => 'required|unique:angkatan',
        'nama_angkatan' => 'required|unique:angkatan',
        'status' => ['required', function ($attribute, $e) { 
            $jml_angkatan_aktif = \App\Models\Angkatan::where('status', 'Aktif')->get('id_angkatan')->count();
            if ($jml_angkatan_aktif > 4) {
                $e('Batas Maksimal '.$attribute.' Aktif untuk Angkatan adalah 4');
            }
            elseif ($jml_angkatan_aktif < 1) {
                $e('Batas Minimal '.$attribute.' Aktif untuk Angkatan adalah 1');
            }
        },
    ],
    ]);

    if($validator->fails()) {
        return response()->json(['status'=>0, 'error'=>$validator->errors()->toArray()]);
    } else {
        $angkatan = new Angkatan;
        $angkatan->id_angkatan = $request->id_angkatan;
        $angkatan->nama_angkatan = $request->nama_angkatan;
        $angkatan->status = $request->status;
        
        $query = $angkatan->save();
        if ($query) {
            return response()->json(['status'=>1, 'msg'=>'Angkatan Berhasil Ditambah']);
        }
    }        
}

here is the page view list of 'angkatan' view of page


Solution

  • i finally got the result what i wanted, using nested if solved the problem, here is the final code

    public function edit(Request $request, $id)
    {
        $angkatan_aktif = \App\Models\Angkatan::where('status', 'Aktif')->get();
    
        $angkatan = Angkatan::find($id);
        $angkatan->id_angkatan = $request->id_angkatan;
        $angkatan->status = $request->status;
        $angkatan->nama_angkatan = $request->nama_angkatan;
        $angkatan->status = $request->status;
    
        if($angkatan_aktif->count() == 4){
    
            if($angkatan->status == "Aktif"){
    
                return redirect('/pengaturan')->with('pesan-angkatan-error', 'Gagal edit! Angkatan Aktif Sudah Mencapai Batas Maksimum : 4');
            
            }
            
            elseif($angkatan->status == "Tidak Aktif"){
    
                $query = $angkatan->save();
                if ($query){
                    return redirect('/pengaturan')->with('pesan-angkatan', 'Angkatan Berhasil Diedit');
                }
    
            }
        }
        elseif($angkatan_aktif->count() < 4){
    
            $query = $angkatan->save();
            if ($query){
                return redirect('/pengaturan')->with('pesan-angkatan', 'Angkatan Berhasil Diedit');
            }
        }
    }  
    

    and here is the result, with blade conditional the add button will not appears, so it will not bother me to use store method, now i cant add 'angkatan' anymore if status 'aktif' = 4 nor have status 'aktif' more than 4 through update method as i wanted.

    if status 'aktif' < 4


    if status 'aktif' = 4