Laravel - Using the Repository Pattern

后端 未结 2 1079
抹茶落季
抹茶落季 2020-12-30 08:00

I am trying to learn the repository pattern, and seem to have got myself a tad confused with how I can use this repository pattern when eager loading relationships and keep

2条回答
  •  感动是毒
    2020-12-30 08:49

    There is a really easy way to do this and it is explored in depth in this link

    http://heera.it/laravel-repository-pattern#.U6XhR1cn-f4

    I was looking for the exact solution and it is working well so far

    so the idea for you would be to declare this in your repository code

    public function __construct(\Category $category)
    {
        $this->category = $category;
    } 
    public function getAllUsers()
    {
        return $this->category->all();
    }
    
    public function __call($method, $args)
    {
        return call_user_func_array([$this->category, $method], $args);
    }
    

    forcing the Model to be called when some functions are missing

提交回复
热议问题