phplaravelrandominvoiceauto-generate

Generate automatically invoice with Laravel


I am working on a very simple project for my school assignment. So it's a house rent site. Everything seems fine but I want create an automatically invoice like "INV0001" but I don't know how to do that. Maybe you guys can help me fix my controller

This is my controller

public function storeSewa(Request $request){
  if ($request->edit=='false') {
  $newdata = new Sewa;
  } else {
  $newdata = Sewa::find($request->id);
  if ($newdata) {
  //
  }else {
  $newdata = new Sewa;}}
  $newdata->invoice = 'INV/'//idk, how?
  $newdata->penyewa_id = $request->penyewa_id;
  $newdata->kamar_id = $request->kamar_id;
  $newdata->tanggal_masuk = $request->tanggal_masuk;
  $newdata->tanggal_keluar = $request->tanggal_keluar;
  $newdata->durasi = $request->durasi;
  $newdata->status = 'Belum Lunas';

  $newdata->save();
  if ($newdata) {
  session()->flash('status', 'Task was successful!');
  session()->flash('type', 'success');
  return Redirect::route('account');
 }
 return 'false';
 }

Well, I am very new to laravel, so is there anyone can help fix my problem in the easiest way?


Solution

  • if you are using database you can use sql auto increment, for this feature you have to have this line in your invoice models migration :

    Schema::create('invoices', function (Blueprint $table) {
                $table->Increments('id');
            });
    

    if you want your invoice numbers, not being like 1,2,3, ... you can use libraries like this:

    https://github.com/oanhnn/laravel-fakeid

    if you are using for test, you can use faker factory: there is a tutorial here:

    https://www.codermen.com/blog/75/how-to-use-faker-with-laravel-5-7-tutorial