Installation:
composer require miladimos/laravel-repository
php artisan repository:install
Create a Repository:
php artisan make:repository Tag
TagRepository (default implementation)TagEloquentRepositoryInterface (contract)TagEloquentRepository (Eloquent-specific implementation)Register the Repository:
app/Providers/RepositoryServiceProvider.php:
protected $repositories = [
TagEloquentRepositoryInterface::class => TagRepository::class,
];
config/app.php under providers.First Use Case:
public function __construct(TagEloquentRepositoryInterface $tagRepo) {
$this->tagRepo = $tagRepo;
}
find(), all(), create()):
$tags = $this->tagRepo->all();
$tag = $this->tagRepo->find(1);
Dependency Injection:
*EloquentRepositoryInterface) into controllers/services, not the concrete class.public function store(Request $request) {
$tag = $this->tagRepo->create($request->all());
}
Custom Methods:
interface TagEloquentRepositoryInterface extends EloquentRepositoryInterface {
public function findBySlug(string $slug);
public function countActiveTags();
}
public function findBySlug(string $slug) {
return $this->model->where('slug', $slug)->first();
}
Service Layer Abstraction:
class TagService {
public function __construct(TagEloquentRepositoryInterface $tagRepo) {
$this->tagRepo = $tagRepo;
}
public function publishTag(int $id) {
$tag = $this->tagRepo->find($id);
$tag->update(['is_published' => true]);
return $tag;
}
}
Pagination and Scopes:
// In TagEloquentRepository
public function scopeActive($query) {
return $query->where('is_active', true);
}
public function getActiveTags() {
return $this->scopeQuery(function($query) {
return $query->active();
})->get();
}
Transactions:
public function transferTagOwnership(int $tagId, int $newOwnerId) {
DB::transaction(function() use ($tagId, $newOwnerId) {
$tag = $this->find($tagId);
$tag->owner_id = $newOwnerId;
$tag->save();
});
}
Testing:
$mockRepo = Mockery::mock(TagEloquentRepositoryInterface::class);
$mockRepo->shouldReceive('find')->andReturn($tag);
$this->app->instance(TagEloquentRepositoryInterface::class, $mockRepo);
API Resources:
return TagResource::collection($this->tagRepo->all());
Event Dispatching:
public function create(array $data) {
$model = parent::create($data);
event(new TagCreated($model));
return $model;
}
Caching:
public function getPopularTags() {
return Cache::remember('popular_tags', now()->addHours(1), function() {
return $this->scopeQuery(function($query) {
return $query->orderBy('views', 'desc')->limit(10);
})->get();
});
}
Missing Provider Registration:
RepositoryServiceProvider in config/app.php will cause ClassNotFoundException.providers in config/app.php.Circular Dependencies:
Observers) can cause circular dependency issues.Overusing Repositories:
User::where(...)->get()). Reserve repositories for complex or reusable logic.Interface Pollution:
TagSearchInterface, TagAnalyticsInterface).Mass Assignment Risks:
create()/update() methods blindly pass input to fill(). Validate input before passing to repositories.$validated = $request->validate([...]);
$this->tagRepo->create($validated);
Repository Not Bound:
BindingResolutionException, check:
$repositories array match exactly.Method Not Found:
php artisan ide-helper:generate to regenerate IDE metadata.Eloquent Model Not Set:
find() or create() fails silently, verify the repository’s $model property is set in the constructor:
public function __construct(Tag $model) {
$this->model = $model;
}
Custom Repository Classes:
BaseRepository for shared functionality:
class BaseSoftDeletesRepository extends BaseRepository {
public function delete($id) {
return $this->model->find($id)->delete();
}
}
Dynamic Model Binding:
resolveModel() to dynamically set the model:
protected function resolveModel($model) {
return app()->make($model);
}
Repository Events:
public function create(array $attributes) {
$model = $this->model->create($attributes);
event(new RepositoryModelCreated($model, $this));
return $model;
}
Repository Middleware:
public function find($id) {
$this->checkPermission('view_tag');
return parent::find($id);
}
Repository Decorators:
class LoggingRepositoryDecorator implements TagEloquentRepositoryInterface {
protected $repository;
public function __construct(TagEloquentRepositoryInterface $repository) {
$this->repository = $repository;
}
public function find($id) {
Log::info("Finding tag {$id}");
return $this->repository->find($id);
}
}
RepositoryServiceProvider:
$this->app->bind(
TagEloquentRepositoryInterface::class,
function($app) {
return new LoggingRepositoryDecorator(
$app->make(TagRepository::class)
);
}
);
Default Repository Class:
BaseRepository class. If missing, create one extending miladimos\Repository\Eloquent\BaseRepository.Interface Naming:
*EloquentRepositoryInterface) is hardcoded in the make:repository command. Customize via the command’s options if needed:
php artisan make:repository Tag --interface=TagRepositoryInterface
Model Resolution:
resolveModel().How can I help you explore Laravel packages today?