Best way to integrate paginated search in cake php 3.0
2 answers
Suppose you have an article controller and you want to paginate your search as optional:
class ArticlesController extends AppController
{
public $paginate = [
'limit' => 25,
'order' => [
'Articles.title' => 'asc'
]
];
public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
}
public function index($search = null)
{
$query = $this->Articles->find();
if($search){
$query->where(['title LIKE' => '%'.$search.'%']);
}
$this->set('articles', $this->paginate($query));
}
}
+3
source to share
I don't know if this is the best solution. But I do this.
In my index.ctp, I put the search form
<?= $this->Form->create('search') ?>
<fieldset>
<?php
echo $this->Form->input('Search');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
And in my controller
if ($this->request->is('post')) {
$pes = '%'.$this->request->data('search').'%';
$customers = $customers->where(['OR' => ['name LIKE' => $pes, 'lastname LIKE' => $pes]]);
}
Hope it works for you.
+1
source to share