Laravel: How to implement Repository Design Pattern with only one repository? - laravel

I've checked many repository design pattern tutorials like
https://asperbrothers.com/blog/implement-repository-pattern-in-laravel/
https://www.larashout.com/how-to-use-repository-pattern-in-laravel
https://laravelarticle.com/repository-design-pattern-in-laravel
https://shishirthedev.medium.com/repository-design-pattern-in-laravel-application-f474798f53ec
But all use multiple repositories with all methods repeated for each model here's an example
class PostRepository implements PostRepositoryInterface
{
public function get($post_id)
{
return Post::find($post_id);
}
public function all()
{
return Post::all();
}
}
interface PostRepositoryInterface
{
public function get($post_id);
public function all();
}
class PostController extends Controller
{
protected $post;
public function __construct(PostRepositoryInterface $post)
{
$this->post = $post;
}
public function index()
{
$data = [
'posts' => $this->post->all()
];
return $data;
}
}
In ReposiroryServiceProvider:
$this->app->bind(
'App\Repositories\PostRepositoryInterface',
'App\Repositories\PostRepository'
);
So now I have UserRepository, PostRepository, CommentRepository .... etc I will have to add the same methods of get, add, .... in all repositories and just change the model name from Post to User .... etc
How can I unify these methods in one file and just pass the model name and use it like this $this->model->all() instead of repeating them in every repository file I create?

You need Abstract Class AbstractRepository, something like this.
Btw, maybe you don't need repository pattern, in Laravel it is not best practice.
abstract class AbstractRepository
{
private $model = null;
//Model::class
abstract public function model(): string
protected function query()
{
if(!$this->model){
$this->model = app($this->model());
}
return $this->model->newQuery()
}
public function all()
{
return $this->query()->all();
}
}

Related

How can I avoid repeating this line in all my methods?

I am working on a blogging application in Laravel 8.
I have a settings table from which I pull the directory name of the current theme.
class ArticlesController extends Controller {
public $theme_directory;
public function index() {
// Theme _directory
$this->theme_directory = Settings::all()[0]->theme_directory;
// All articles
$articles = Article::all();
return view('themes/' . $this->theme_directory . '/templates/index', ['articles' => $articles]);
}
public function show($slug) {
// Theme _directory
$this->theme_directory = Settings::all()[0]->theme_directory;
// Single article
$article = Article::where('slug', $slug)->first();
return view('themes/' . $this->theme_directory . '/templates/single', ['article' => $article]);
}
}
The problem
A you can see, the line $this->theme_directory = Settings::all()[0]->theme_directory is repeted in both methods (and would be repeted in others in the same way).
Question
How can I avoid this repetition (and make my code DRY)?
Inheritance approach
Inheritance for a controller would avoid you from repeating it.
abstract class CmsController extends Controller{
protected $themeDirectory;
public function __construct()
{
$this->themeDirectory= Settings::first()->theme_directory ?? null;
}
}
Extend it and you can access it like you have always done.
class ArticlesController extends CmsController
{
public function index() {
dd($this->themeDirectory);
}
}
Trait
Use traits which is partial classes, done by just fetching it, as it is used in different controllers the performance is similar to saving it to an property as it is never reused.
trait Themeable
{
public function getThemeDirectory()
{
return Settings::first()->theme_directory ?? null;
}
}
class ArticlesController extends CmsController
{
use Themeable;
public function index() {
dd($this->getThemeDirectory());
}
}
Static function on model
If your models does not contain to much logic, a static function on models could also be a solution.
class Setting extends model
{
public static function themeDirectory()
{
return static::first()->theme_directory ?? null;
}
}
class ArticlesController extends CmsController
{
use Themeable;
public function index() {
dd(Setting::themeDirectory());
}
}

Override Eloquent Relation Create Method

I want to override create method, but with relation, it didn't touch the create method.
There are Two Models:
class User extends Model
{
public function user_detail()
{
return $this->hasOne(UserDetail::class);
}
}
class UserDetail extends Model
{
public static function create(array $attributes = [])
{
//I was trying to do something like
/*
if(isset($attributes['last_name']) && isset($attributes['first_name']))
{
$attributes['full_name']=$attributes['first_name'].' '.$attributes['last_name'];
}
unset($attributes['first_name'],$attributes['last_name']);
*/
Log::debug('create:',$attributes);
$model = static::query()->create($attributes);
return $model;
}
}
When I use UserDetail::create($validated), and there is a log in laravel.log, so I know the code touched my custom create method.
But if I use
$user = User::create($validated);
$user->user_detail()->create($validated);
There is no log in laravel.log, which means laravel didn't touch the create method, then how I supposed to do to override create method under this circumstance?(I'm using laravel 5.7)
Thank you #Jonas Staudenmeir, after I read the documentation, here is my solution.
If the $attributes are not in protected $fillable array, then I do it in the __construct method.
class UserDetail extends Model
{
protected $fillable=['full_name','updated_ip','created_ip'];
public function __construct(array $attributes = [])
{
if (isset($attributes['first_name']) && isset($attributes['last_name'])) {
$attributes['full_name'] = $attributes['first_name'].' '.$attributes['last_name'];
}
parent::__construct($attributes);
}
}
Otherwise, I do it in Observer.
namespace App\Observers;
use App\Models\UserDetail;
class UserDetailObserver
{
public function creating(UserDetail $userDetail)
{
$userDetail->created_ip = request()->ip();
}
public function updating(UserDetail $userDetail)
{
$userDetail->updated_ip = request()->ip();
}
}
Register Observer in AppServiceProvider.
namespace App\Providers;
use App\Models\UserDetail;
use App\Observers\UserDetailObserver;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
UserDetail::observe(UserDetailObserver::class);
}
}
I choose Observer instead of Event&Listener is for easy maintenance.

Use Factory Pattern in Laravel

I am working on a Laravel project and want to implement Factory and Repository pattern in my project. However, I still don't know where should I use Factory pattern? It's still very confusing. I understand that it's not necessary to use the Factory pattern for every single project but as a part of my study, I want to learn more about the combination of this 2 patterns. This is my code example. Hope I can get help and explains from you guys. Thank you.
class ProductFactory
{
public function create()
{
return new Product;
}
}
class ProductRepository implements ProductRepositoryInterface
{
protected $productFactory;
public function __contruct(
ProductFactory $productFactory
)
{
$this->productFactory = $productFactory
}
public function all()
{
return Product::all();
}
public function loadById($id);
{
return Product::find($id);
}
public function save($data,$id=NULL)
{
if($id != NULL){
$product = $this->loadById($id)
}
else{
$product = $this->productFactory->create();
}
return $product->fill($data)->save();
}
.....
}
I think you need to split the factory and repository. For example:
class ProductRepository implements ProductRepositoryInterface
{
protected $product;
public function __contruct(Product $product)
{
$this->product = $product;
}
//...
}
But not necessary for DI in repository, your methods like all(), find() and etc. put in AbstractRepository. Your ProductRepositry:
class ProductRepository extends AbstractRepository implements ProductRepositoryInterface
{
public function __contruct()
{
//model property in AbstractRepository
$this->model = new Product();
}
}
If you want to write test for it, you may use laravel container:
AppServiceProvider:
$this->app->bind(Product::class, function ($app) {
return new Product();
});
ProductRepository:
$this->model = app()->make(Product::class);
PS: i think factory useless, in most cases i use container. But in some difficult cases i use factory, for example:
class ProductFactory
{
public static function create(int $type, array $attributes): ProductInterface
{
switch ($type) {
case 'market':
return new MarketProductModel($attributes);
break;
case 'shop':
$model = new ShopProductModel($attributes);
$model->setMathCoef(1.2);
return $model;
break;
case 'something':
return new SomethingProductModel($attributes);
break;
}
}
}

Laravel Multiple module using one Controller

Now I have few modules created like Product, Sale, Category. I found out they actually using same function with similar process. For example update() in Controller :
Category
public function update($id)
{
$instance = Category::findOrFail($id);
$instance->fill(Input::all())->save();
}
Product
public function update($id)
{
$instance = Product::findOrFail($id);
$instance->fill(Input::all())->save();
}
How can I join it together to BaseController by just make the Model dynamic?
Something like this:
abstract class ResourceController extends BaseController
{
protected $entity;
public function __construct(Model $entity){ //or Eloquent, depending on your import alias
$this->entity = $entity;
}
public function update($id)
{
$instance = $this->entity->findOrFail( $id );
$instance->fill( Input::all() )->save();
}
}
class ProductController extends ResourceController{
public function __construct(Product $entity){
parent::__construct($entity);
}
}
class CategoryController extends ResourceController{
public function __construct(Category $entity){
parent::__construct($entity);
}
}

setting up DB connections on the fly for models?

I'm creating a multi-tenant application and assigning a DB to each user and his/her sub-users. I first fetch the DB name / password / username for this user and then I need to make it the default for all DB operations to follow. How to accomplish this in Laravel 4?
You can use the connection method:
User::connection('master')->where('name', $name)->get();
And you can use the repository pattern to help you with this too:
class PostRepository {
private $connection = 'default-connection';
public function setConnetion($connection)
{
$this->connection = $connection;
}
public function all()
{
return Post::on($this->connection)->all();
}
public function query()
{
return Post::on($this->connection);
}
}
It will let you:
$post = new PostRepository;
$post->setConnection('new-connection');
return $post->query()->where('name', $name)->get();
or
return $post->all();
You can also create a BaseModel and do inside it:
class BaseModel extends Eloquent {
private $connection = 'default-connection';
public function __construct()
{
if (Auth::check())
{
$this->setConnection(Auth::user()->database_connection);
}
}
}
class Post extends BaseModel {
}

Resources