HARI KE-13 PRAKERIN #13

 

6/21/2024

Assalamualaikum wr wb

Pagi hari seperti biasa apel ,lanjut ngoding

membuat table data tempat prakerin


1.migration

public function up()
    {
        Schema::create('tempatPrakerin', function (Blueprint $table) {
            $table->id();
            $table->string('nama_dudi');
            $table->string('nama_pimpinan');
            $table->string('alamat_dudi');
            $table->integer('jmlh_kuota');
            $table->integer('kuota_terisi');
            $table->integer('sisa_kuota');
            $table->softDeletes();
            $table->timestamps();
        });
    }

2.Model 

protected $table = 'tempatPrakerin';
    protected $quarded = ['id'];
    protected $fillable = ['nama_dudi','nama_pimpinan','alamat_dudi','jmlh_kuota','kuota_terisi','sisa_kuota'];

    public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            $model->sisa_kuota = $model->jmlh_kuota - $model->kuota_terisi;
        });
    }

fungsi boot pada model ,untuk menghitung sisa kuota pada setiap tempat dudi

contoh : jumlah kuota 6 - kuota terisi 3 = sisa kuota 3 (otomatis akan mengurangi)

3.Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\tempatPrakerin;

class tempatPrakerinController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('tempat-dudi.index')->with([
            'tempatPrakerin' => tempatPrakerin::all(),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('tempat-dudi.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'nama_dudi' => 'required',
            'nama_pimpinan'  => 'required',
            'alamat_dudi'  => 'required',
            'jmlh_kuota' => 'required|numeric|max:12',
            'kuota_terisi' => 'required|numeric|max:12',
        ],[
            'kuota_terisi.required' => 'Silahkan Isi Angka 0 Jika Tempat Dudi Belum Terisi Oleh Siswa',
            'jmlh_kuota.max' => 'Maximal Hanya 12 Siswa',
            'kuota_terisi.max' => 'Maximal Hanya 12 Siswa',
        ]);

        $sisa_kuota = $request->jmlh_kuota - $request->kuota_terisi;

        $tempatPrakerin = new tempatPrakerin;
        $tempatPrakerin->nama_dudi = $request->nama_dudi;
        $tempatPrakerin->nama_pimpinan = $request->nama_pimpinan;
        $tempatPrakerin->alamat_dudi = $request->alamat_dudi;
        $tempatPrakerin->jmlh_kuota = $request->jmlh_kuota;
        $tempatPrakerin->kuota_terisi = $request->kuota_terisi;
        $tempatPrakerin->sisa_kuota = $request->sisa_kuota;
        $tempatPrakerin->save();
        return to_route('tempat-dudi.index')->with('success','Data Berhasil Ditambahkan');
    }

    /**
     * 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)
    {
        return view('tempat-dudi.edit')->with([
            'tempatPrakerin' => tempatPrakerin::find($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)
    {
        $request->validate([
            'nama_dudi' => 'required',
            'nama_pimpinan'  => 'required',
            'alamat_dudi'  => 'required',
            'jmlh_kuota' => 'required|numeric|max:12',
            'kuota_terisi' => 'required|numeric|max:12',
        ],[
            'kuota_terisi.required' => 'Silahkan Isi Angka 0 Jika Tempat Dudi Belum Terisi Oleh Siswa',
            'jmlh_kuota.max' => 'Maximal Hanya 12 Siswa',
            'kuota_terisi.max' => 'Maximal Hanya 12 Siswa',
        ]);

        $sisa_kuota = $request->jmlh_kuota - $request->kuota_terisi;

        $tempatPrakerin = tempatPrakerin::find($id);
        $tempatPrakerin->nama_dudi = $request->nama_dudi;
        $tempatPrakerin->nama_pimpinan = $request->nama_pimpinan;
        $tempatPrakerin->alamat_dudi = $request->alamat_dudi;
        $tempatPrakerin->jmlh_kuota = $request->jmlh_kuota;
        $tempatPrakerin->kuota_terisi = $request->kuota_terisi;
        $tempatPrakerin->sisa_kuota = $request->sisa_kuota;
        $tempatPrakerin->save();
        return to_route('tempat-dudi.index')->with('success','Data Berhasil Diupdate');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $tempatPrakerin = tempatPrakerin::find($id);
        $tempatPrakerin->delete();
        return back()->with('success','Data Berhasil Dihapus');
    }
}


sama seperti model pada controller saya juga memasukan fungsi agar otomatis mengurangi

maximal hanya 12 siswa






Wassalamualaikum wr wb

Komentar

Postingan populer dari blog ini

PRAKERIN HARI KE 54-58