How to make autonumber restart every year in Laravel/PHP?

455 Views Asked by At

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

2

There are 2 best solutions below

0
On

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.

0
On

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');
}