Filter index by a select option in cakephp 4.2

I have the index template, there I have a select of the users to filter the grid with the user selected, but when I click on the button to execute the query it doesn’t do anything.
This is the code for index action;

    public function index()
    {
		$this->paginate = [
			'contain' => ['Users'],
		];
		if ($this->request->is('post') && !empty($this->request->getData('user_id'))) {
			$observations = $this->paginate($this->Observations->find('all')->where(['user_id' => $this->request->getData('user_id')]));
		}else{
			$observations = $this->paginate($this->Observations->find('all'));
		}

	        $users = $this->Observations->Users->find('list', ['limit' => 200]);
		$this->set(compact('observations', 'users'));
    }

And this is the code for index template:

<?php
/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\Observation[]|\Cake\Collection\CollectionInterface $observations
 */
?>
<div class="observations index content">
    <?= $this->Html->link(__('Agregar Observación'), ['action' => 'add'], ['class' => 'button float-right']) ?>
	<div class="observations form content">
		<?= $this->Form->create($observations) ?>
		<fieldset>
			<legend><?= __('Filtro') ?></legend>
			<?php
				echo $this->Form->control('user_id', ['label' => 'Usuario', 'options' => $users, 'empty' => 'Seleccione un usuario']);
			?>
		</fieldset>
		<?= $this->Form->button(__('Consultar')) ?>
		<?= $this->Form->end() ?>
	</div>
    <h3><?= __('Observations') ?></h3>
    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th><?= $this->Paginator->sort('user_id', 'Usuario') ?></th>
                    <th><?= $this->Paginator->sort('observation', 'Observación') ?></th>
                    <th><?= $this->Paginator->sort('created', 'Creado') ?></th>
                    <th><?= $this->Paginator->sort('modified', 'Modificado') ?></th>
                    <th class="actions"><?= __('Acciones') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($observations as $observation): ?>
                <tr>
                    <td><?= $observation->has('user') ? $this->Html->link($observation->user->username, ['controller' => 'Users', 'action' => 'view', $observation->user->id]) : '' ?></td>
                    <td><?= h($observation->observation) ?></td>
                    <td><?= h($observation->created) ?></td>
                    <td><?= h($observation->modified) ?></td>
                    <td class="actions">
                        <?= $this->Html->link(__('Ver'), ['action' => 'view', $observation->id]) ?>
                        <?= $this->Form->postLink(__('Eliminar'), ['action' => 'delete', $observation->id], ['confirm' => __('¿Desea eliminar esta observación?', $observation->id)]) ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->first('<< ' . __('Primera')) ?>
            <?= $this->Paginator->prev('< ' . __('Anterior')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('Siguiente') . ' >') ?>
            <?= $this->Paginator->last(__('Última') . ' >>') ?>
        </ul>
        <p><?= $this->Paginator->counter(__('Página {{page}} de {{pages}}, mostrando {{current}} registro(s) de {{count}} total')) ?></p>
    </div>
</div>

When you say “doesn’t do anything”, do you mean that the page just sits there, or it reloads with all the records loaded, ignoring the filter?

It reloads ignoring the filter

So, it does something, just not what you want. :slight_smile:

You have an if statement there with two parts. Have you checked the results of each part independently to see which one’s failing? Or is that passing and the find is somehow not returning what you expect? These are all straight-forward debugging steps that you can take to improve your question and help us help you.

I solved it deleting the part where it says $this->request->is(‘post’), now the code for index action is:

    public function index()
    {
		$this->paginate = [
			'contain' => ['Users'],
		];
		if (!empty($this->request->getQuery('user_id'))) {
			$observations = $this->paginate($this->Observations->find('all')->where(['user_id' => $this->request->getQuery('user_id')]));
		}else{
			$observations = $this->paginate($this->Observations->find('all'));
		}

	        $users = $this->Observations->Users->find('list', ['limit' => 200]);
		$this->set(compact('observations', 'users'));
    }

Might be a patch request instead of post? And I see you’re using $this->request instead of $this->getRequest(), not sure if that might affect this in any way.

And if you want to simplify your code a bit, you might use:

$observations = $this->paginate($this->Observations->find('all'));
if (!empty($this->request->getQuery('user_id'))) {
	$observations = $observations->where(['user_id' => $this->request->getQuery('user_id')]));
}