skip to Main Content

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


  1. You are defining $this->no_antrian in a class property and in the else block in variable.
    When you assign :

    $data->no_antrian = ($no_antrian);
    

    You are using only variable so it could be undefined.

    try to change in the else block :

    $no_antrian = $kode_awal . $angka;
    

    by :

    $this->no_antrian = $kode_awal . $angka;
    

    and :

    $data->no_antrian       = ($no_antrian);
    

    by :

    $data->no_antrian       = $this->no_antrian;
    
    Login or Signup to reply.
  2. TL:TR; You are facing Undefined variable $no_antrian because that variable is assigned only in else branch of your condition.


    Some issues with your current code:

    1. The $tanggal_antrian ($queue_date) variable is assigned only in else branch, in other you assign $this->$tanggal_antrian and that’s why you face undefined variable error in $data->tanggal_antrian = ($tanggal_antrian); line. I assume you intended to assign local variable in both cases.
    2. Not sure what you wanted to achieve by adding brackets in the $data->tanggal_antrian = ($tanggal_antrian); line. Whatever was the reason, these brackets are pointless here.
    3. No error handling, which is bad idea -> what happens if the save operation fails?
    4. Finally, code isn’t DRY (Don’t Repeat Yourself); there are repetitive blocks that can be refactored.

    I’d also suggest you work on your coding style:

    1. You mix naming conventions (snake_case vs camelCase). Stick to one.
    2. I recommend you resist from using non-English to name your variables/methods as this, combined with the fact there’s no PHPDoc blocks makes the code hard to follow.

    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 to Queue or so too):

    public function store(Request $request)
    {
        // Get the latest queue entry based on the service and current date
        $latest_queue = Antrian::where('service', $request->service)
                              ->where('queue_date', now()->toDateString())
                              ->latest('queue_id')
                              ->first();
    
        $queue_number = '';
        $queue_date = now()->toDateString();
    
        // Generate new queue number based on the latest entry and type of service
        if (!$latest_queue) {
            $prefixes = ['child'   => 'A',
                         'mother'  => 'B',
                         'elderly' => 'C'];
            $queue_number = isset($prefixes[$request->service])
                ? $prefixes[$request->service] . '1'
                : 'Z1';
        } else {
            $initial_code = substr($latest_queue->queue_number, 0, 1);
            $number = (int)substr($latest_queue->queue_number, 1);
            $number++;
            $queue_number = "{$initial_code}{$number}";
        }
    
        // Create and save new Queue entry
        $data = new Antrian([
            'queue_number'  => $queue_number,
            'name'          => $request->name,
            'mobile_number' => $request->mobile_number,
            'service'       => $request->service,
            'queue_date'    => $queue_date,
        ]);
    
        $redirect_data = $data->save()
            ? ['success' => 'Data Successfully Added']
            : ['error' => 'Data Failed to Add'];
        return redirect()->route('queueUser')->with($redirect_data);
    }
    

    Not sure what PHP version you use but if 8, I’d replace

    $queue_number = isset($prefixes[$request->service])
        ? $prefixes[$request->service] . '1'
        : 'Z1';
    

    with shoter form:

    $queue_number = ($prefixes[$request->service] ?? 'Z') . '1';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search