Reputation: 3
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?
Upvotes: -1
Views: 1809
Reputation: 505
I am also researching on this but for such numbers with prefixes, if not a multi-tenant system, the prefix can be hard-coded as you have done or be read from a config file. However, in most transactional database engines like MariaDb's InnoDb, you cannot reliably count on the auto-increment value as they have a tendency to skip their serials.
I am thinking of creating a config-table as well as a serial-lookup table. This last one will be 'selected for updates' to maintain integrity on lookups and increments.
Upvotes: 0
Reputation: 5055
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
Upvotes: 1