Skip to main content
Version: 9.x

Search Query Parameter

Below we'll see how to set up a Search Query Parameter, on a Model:

  1. Add searchable Fields on the Model Repository, all the other steps are normal steps
<?php

namespace App\Containers\User\Data\Repositories;

use App\Containers\User\Contracts\UserRepositoryInterface;
use App\Ship\Parents\Repositories\Repository;

class UserRepository extends Repository implements UserRepositoryInterface
{

protected $fieldSearchable = [
'name' => 'like',
'id' => '=',
'email' => '=',
];

}
  1. Create basic list and search Task
<?php

namespace App\Containers\User\Tasks;

use App\Containers\User\Contracts\UserRepositoryInterface;
use App\Port\Action\Abstracts\Action;

class ListUsersTask extends Action
{
private $userRepository;

public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}

public function run($order = true)
{
return $this->userRepository->paginate();
}
}

  1. Create basic Action to call that basic Task, and maybe other Tasks later in the future when needed
<?php

namespace App\Containers\User\Actions;

use App\Containers\User\Tasks\ListUsersTask;
use App\Port\Action\Abstracts\Action;

class ListAndSearchUsersAction extends Action
{

private $listUsersTask;

public function __construct(ListUsersTask $listUsersTask)
{
$this->listUsersTask = $listUsersTask;
}

public function run($order = true)
{
return $this->listUsersTask->run($order);
}
}

  1. Use the Action from a Controller

<?php

public function listAllUsers()
{
$users = Apiato::call('User@ListAndSearchUsersAction');

return $this->response->paginator($users, new UserTransformer());
}

  1. Call it from anywhere as follows: [GET] http://api.apiato.com/[email protected]