0

I made code like this in Models Number_npe:

public function nomor_akhir()
{
    $query = DB::table('nomor_npe')
        ->select('*')
        ->orderBy('id','DESC')
        ->first();
    return $query;
}

Then the Controllers:

public function nomor_npe_store(Request $req)
{
    $tanggal_npe = $req->input('tanggal_npe');
    $pesan  = new Nomor_npe();
    $check  = $pesan->nomor_akhir();
    if($check) {
        $nomor_npe    = $check->nomor_npe+1;
    }else{
        $nomor_npe    = 1;
    }

    DB::table('nomor_npe')->insert([
        'nomor_npe'    => $nomor_npe,
        'tanggal_npe'    => $tanggal_npe
    ]);

    return redirect('nomor_npe')->with('success','Nomor NPE berhasil ditambahkan');
}

The Add NPE Number display looks like this:

enter image description here

When I click Save, the number_npe has been successfully added automatically.

enter image description here

But I want to make when the year changes, the number_npe restarts automatically from 1 again ... Please help everyone who knows

Paul T.
  • 4,703
  • 11
  • 25
  • 29
Ranmouri
  • 1
  • 3
  • 1
    What if you want to add another year and it doenst exists in DB? you will get unordered nomor_npe. For example id 5 with nomor_npe 1 and year 2022-MM-DD. Add another row id 6 with 2022-MM-DD will results nomor_npe 6 instead of 2. Based on your model "nomor_akhir" which is get latest id from DB – Localhousee Apr 13 '21 at 00:59

2 Answers2

1

I have to write this as an answer, but it is not 100% an answer to your code, these are just tips for you to have better code. (So if anyone sees this too, they are aware too)

First of all, avoid 100% writing code in other language than English, as we are following it (we do not speak your language) and we do not understand nearly anything unless we use a Translator...

So, if you are going to use Laravel, try to avoid using DB, when you can just use the Model (hopefully you have created it...).

So your class should look like this:

public function lastNumber()
{
    return NomorNpe::orderByDesc('id')->first();
}

Then your controller should be like:

public function store(Request $request, NomorNpe $nomor_npe)
{
    NomorNpe::create([
        'nomor_npe' => $nomor_npe->lastNumber() ? $nomor_npe->lastNumber()->nomor_npe + 1 : 1,
        'tanggal_npe' => $request->input('tanggal_npe')
    ]);

    return redirect('nomor_npe')->with('success', 'Nomor NPE berhasil ditambahkan');
}

See how I reduced everything from 13 lines of code to 5 lines of code and is 100% readable... (Or 9 lines to 2)


Make sure to use what Laravel brings you as "default" for it, use Models not DB::table('xxx'), take advantage of Eloquent.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
0

Use this code for starting the number from 1, when the year changed:

public function nomor_npe_store(Request $req) {
    $tanggal_npe = $req->input('tanggal_npe');
    //---Current Date
    $date = date('Y-m-d', time());

    //---NOMOR NPE
    $nomor_npe = DB('number_npe')->whereYear('tanggal_npe', $date)->max('normor_npe');
    if (!$nomor_npe) {
        $nomor_npe = 1;
    } else {
        $nomor_npe++;
    }
    DB::table('nomor_npe')->insert([
        'nomor_npe'    => $nomor_npe,
        'tanggal_npe'    => $tanggal_npe
    ]);

    return redirect('nomor_npe')->with('success','Nomor NPE berhasil ditambahkan');
}
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38