Laravel - Using the Repository Pattern

后端 未结 2 1078
抹茶落季
抹茶落季 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:32

    You are over thinking, repository is just a link/bridge between your controller and model and hence controller uses repository class instead of the model directly and in that repository you may declare your methods using the model from there, for example:

    Now implement the interface in the repository class:

    get();
        }
    
        public function find($id)
        {
            return Cat::find($id):
        }
    }
    

    To use it in your controller:

    cat = $category;
        }
    
        public function getCatWith()
        {
            $catsProd = $this->cat->getCategoriesWith('products');
            return $catsProd;
        }
    
        // use any method from your category
        public function getAll()
        {
            $categories = $this->cat->all();
    
            return View::make('category.index', compact('categories'));
        }
    
    }
    

    Note: Omitted the IoC binding of the repository because this is not your problem and you know that.

    Update: I've written an article here: LARAVEL – USING REPOSITORY PATTERN.

提交回复
热议问题