Get last record in spesific field

I want to add new record that contain string serial number, txtbox serial number has unique so I will put reference the last string serial number, and then user guide enter next string serial number based on show guide the last input serial number. Below this code

add Controller

namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;

public function add()
    {
		$query = $this->Evidences->find()
					->select(['kode'])
					->last();
		$evidence = $this->Evidences->newEntity();
		$evidence->user_id = $this->request->getSession()->read('Auth.User.ID');
        if ($this->request->is('post')) {
            $evidence = $this->Evidences->patchEntity($evidence, $this->request->getData());
            if ($this->Evidences->save($evidence)) {
                $this->Flash->success(__('Eviden berhasil disimpan.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Eviden gagal disimpan, saran kode Eviden harus berbeda. Coba lagi.'));
        }
        $users = $this->Evidences->Users->find('list', ['limit' => 200]);
        $this->set(compact('query', 'evidence', 'users'));
    }

next I put looping foreach the query above to get the last record serial number

add.ctp

<?php
  echo $this->Form->create($evidence)."\n";
  echo "<br>";
?>
<?php foreach ($query as $evidence): ?>
  <p style="font-family:sans-serif;font-size:13px;font-color:#FFF;">Entry the Evidence Code after this&nbsp;:</p>&nbsp;&nbsp;<?= h($evidence->kode) ?>
<?php endforeach; ?>
<br>
<?php
  echo "<p style='font-family:sans-serif;font-size:13px;font-color:#FFF;'>Kode Evidence</p>".$this->Form->control('kode', array('type' => 'text', 'id' => 'kode', 'autocomplete' => 'off', 'label' => false, 'charwidth' => '5', 'style' => 'width:200px;'))."\n";
  echo "<br>";
  echo "<p style='font-family:sans-serif;font-size:13px;font-color:#FFF;'>Gejala Evidence</p>".$this->Form->control('eviden', array('type' => 'text', 'id' => 'eviden', 'autocomplete' => 'off', 'label' => false, 'style' => 'width:200px;'))."\n";
  echo "<br>";
  echo $this->Form->submit('TAMBAH', array('style' => 'width:100px;'))."\n";
  echo $this->Form->end()."\n";
?>

the result of looping foreach is empty or null, below sample record of Evidence in MySQL

+----+----------+----------+--------------+
| ID | user_id  |   kode   |    eviden    |
+----+----------+----------+--------------+
|  1 |    1     |    EV001 | Selamat Pagi |
+----+----------+----------+--------------+

the goal is I want the last record EV001 is shown at position looping foreach,
for the user guide easy to entry next record serial number.

I hope the someone can help me, thanx

There should be no need to foreach over $query, it should be an entity because you’ve used ->last().

Related note, you should perhaps put an order clause on that query, or else you get the last one based on whatever the database decides to order the results by.