Search Query Parameter 本文未发布 发布文章

未匹配的标注

Below we’ll see how to setup 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' => '=',
    ];

}

2.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();
    }
}

3.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);
    }
} 

4.Use the Action from a Controller

<?php

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

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

5.Call it from anywhere as follow: [GET] http://api.apiato.com/users?search=Mahmoud@apiato.com

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~