Search Query Parameter
Below we'll see how to set up a Search Query Parameter, on a Model:
- 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' => '=',
];
}
- 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();
}
}
- 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);
}
}
- Use the Action from a Controller
<?php
public function listAllUsers()
{
$users = Apiato::call('User@ListAndSearchUsersAction');
return $this->response->paginator($users, new UserTransformer());
}
- Call it from anywhere as follows: [GET]
http://api.apiato.com/[email protected]