如何使用 Laravel 存储库模式可搜索数组?

发布于 2025-01-16 04:07:01 字数 1641 浏览 4 评论 0原文

我正在使用 laravel-repository 模式,我有一个 api 负责让所有用户工作正常,如果我们默认使用该包,搜索应该支持我在存储库类中设置 $fieldSearchable 数组。

我点击了这样的 api localhost.com/api/lists?search=foo,它不起作用,你能帮我看看我在哪里出错了

UserController.php

public function __construct(UserRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }
 public function getUsers(){
        $data = $this->repository->show();
        return response()->json(fractal($data, new UserTransformer()));
    }

UserRepositoryInterface.php

interface UserRepositoryInterface extends RepositoryInterface
{
    public function show();
}

UserRepository.php

<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\User as AppUser;
use App\UserSection;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\DB;

/**
 * Class UserRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class UserRepository extends BaseRepository implements UserRepositoryInterface
{

  protected $fieldSearchable = ['phone_number'];
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AppUser::class;
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

  

    public function show(){
      return $this->model()::get();
    }
    
}


I am using laravel-repository pattern ,i have one api which is responsible for getting all users it's working fine ,if we are using that package by default search should support for that i set $fieldSearchable array in the repository class.

i hit an api like this localhost.com/api/lists?search=foo,it's not working can you please help me where did i mistake

UserController.php

public function __construct(UserRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }
 public function getUsers(){
        $data = $this->repository->show();
        return response()->json(fractal($data, new UserTransformer()));
    }

UserRepositoryInterface.php

interface UserRepositoryInterface extends RepositoryInterface
{
    public function show();
}

UserRepository.php

<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\User as AppUser;
use App\UserSection;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\DB;

/**
 * Class UserRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class UserRepository extends BaseRepository implements UserRepositoryInterface
{

  protected $fieldSearchable = ['phone_number'];
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AppUser::class;
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

  

    public function show(){
      return $this->model()::get();
    }
    
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2025-01-23 04:07:01

它可以通过利用预定义的方法来解决不需要编写 show() 函数逻辑,因为默认情况下 l5-Repository 模式包含一些获取所有数据 all()paginate() 的方法

在您的控制器中,在 getUsers() 中编写如下代码:

$data = $this->repository->all();
or
$data = $this->repository->paginate('25');

all() 用于从数据库中获取所有数据,paginate($limit)< /code> 是根据限制获取每页的数据。

如果您使用上述任何一种方法,则自动搜索功能将起作用

It maybe resolved by utilising pre-defined methods No need to write show() function logic because by default l5-Repository pattern contains some methods to get all the data all() or paginate().

In your controller write your code like this in getUsers():

$data = $this->repository->all();
or
$data = $this->repository->paginate('25');

all() is for fetching all the data from the database, and paginate($limit) is fetch the data per page based on the limit.

If you are using any one of the above mentioned method then automatically search functionality will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文