I want to create a queuing system in Laravel but I have problems with coding and don’t understand how to set up the coding correctly. Can anyone help me?
public function store(Request $request ){
$latestAntrian = Antrian::where('layanan', $this->layanan)
->where('tanggal_antrian', now()->toDateString())
->latest('id_antrian')
->first();
if (!$latestAntrian) {
if($this->layanan === 'anak'){
$this->no_antrian = 'A1';
} elseif ($this->layanan === 'ibu'){
$this->no_antrian = 'B1';
} elseif ($this->layanan === 'lansia'){
$this->no_antrian = 'C1';
}
$this->tanggal_antrian = now()->toDateString();
} else {
$kode_awal = substr($latestAntrian->no_antrian, 0, 1);
$angka = (int) substr($latestAntrian->no_antrian, 1);
$angka +=1;
$no_antrian = $kode_awal . $angka;
$tanggal_antrian = $latestAntrian->tanggal_antrian;
}
$data = new Antrian;
$data->no_antrian = ($no_antrian);
$data->nama = $request->get('nama');
$data->no_hp = $request->get('no_hp');
$data->layanan = $request->get('layanan');
$data->tanggal_antrian = ($tanggal_antrian);
$data->save();
Alert::success('Data Berhasil Ditambah');
return redirect()->route('antrianuser')->with([
'success' => 'Data Berhasil Di Tambah'
]);
}
help me by improving this code
2
Answers
You are defining $this->no_antrian in a class property and in the else block in variable.
When you assign :
You are using only variable so it could be undefined.
try to change in the else block :
by :
and :
by :
TL:TR; You are facing
Undefined variable $no_antrian
because that variable is assigned only inelse
branch of your condition.Some issues with your current code:
$tanggal_antrian
($queue_date
) variable is assigned only inelse
branch, in other you assign$this->$tanggal_antrian
and that’s why you faceundefined variable
error in$data->tanggal_antrian = ($tanggal_antrian);
line. I assume you intended to assign local variable in both cases.$data->tanggal_antrian = ($tanggal_antrian);
line. Whatever was the reason, these brackets are pointless here.I’d also suggest you work on your coding style:
snake_case
vscamelCase
). Stick to one.So I’d tidy the code more/less like this (I replaced all non-English names with the translations, with the exception for
Antrian
model class, which you should rename toQueue
or so too):Not sure what PHP version you use but if 8, I’d replace
with shoter form: