arifurrahmansw/laravel-repository
最新稳定版本:v1.2.3
Composer 安装命令:
composer require arifurrahmansw/laravel-repository
包简介
A Laravel package that provides a reusable and extendable repository pattern implementation.
README 文档
README
A Laravel package that provides a clean, reusable, and extendable Repository Pattern implementation with artisan command support. Designed to keep your business logic separate from data access.
---📦 Package Name
arifurrahmansw/laravel-repository
Build maintainable Laravel apps using the repository pattern with ease.
🚀 Features
- 🧠 Simple command to generate repository pattern files
- 📁 Automatically generates:
- Repository Interface
- Repository Class (extends
BaseRepository) - Optional Eloquent Model
- 🔌 Auto-binds interface to implementation in your
RepositoryServiceProvider - ⚙️ Customizable stub publishing
- 🧪 Compatible with Laravel 10, 11, 12+
📥 Installation
Install the package via Composer:
composer require arifurrahmansw/laravel-repository
Publish the package assets (provider, stubs, etc.):
php artisan vendor:publish --tag=laravel-repository-provider
🔧 Configuration
This package supports Laravel's auto-discovery out of the box.
If you want to register the service provider manually, add it to your config/app.php providers array:
'providers' => [ // Other service providers... App\Providers\RepositoryServiceProvider::class, ],
✨ Usage
📁 Generate a Repository
To generate a full repository structure for a model:
php artisan make:repo User
This will:
✅ Create App\Models\User (if it doesn’t exist)
✅ Generate UserInterface.php and UserAbstract.php under App\Repositories\User
✅ Register binding automatically inside App\Providers\RepositoryServiceProvider
🔀 Generate Repository Without Model
php artisan make:repo User --no-model
🗂 Directory Structure (Generated)
app/
├── Models/
│ └── User.php
├── Http/
│ └── Controllers/
│ └── UserController.php
├── Repositories/
│ └── User/
│ ├── UserInterface.php
│ └── UserAbstract.php
└── Providers/
└── RepositoryServiceProvider.php
🔁 Interface Binding
The package auto-registers this in your RepositoryServiceProvider:
$this->app->bind( \App\Repositories\User\UserInterface::class, \App\Repositories\User\UserAbstract::class );
All generated repositories extend ArifurRahmanSw\Repository\BaseRepository.
✨ Available Methods
public function paginate(int $limit = 10): LengthAwarePaginator; public function all(): Collection; public function combo(string $key = 'id', string $value = 'name'): Collection; public function find(int $id): ?Model; public function findBy(string $field, $value): ?Model; public function store(array $data): object; public function update(int $id, array $data): object; public function destroy(int $id): object; public function statusUpdate(int $id): object; public function search(array $filters = [], int $limit = 10): LengthAwarePaginator; public function restore(int $id): object;
🛠 Helper Response Methods
formatResponse(bool $status, string $message, string $redirect_to, $data = null); successResponse(int $code, string $message, $data = null); jsonResponse(string $message = null, array|object $data = [], int $statusCode = 200);
🧪 Example Usage in Controller
use App\Repositories\User\UserInterface; class UserController extends Controller { /** * The repository instance. * * @var UserInterface */ protected UserInterface $user; public function __construct(UserInterface $user) { $this->user = $user; } /** * Display a listing of the resource. */ public function index() { $data = $this->user->paginate(10); return view('users.index', compact('data')); } /** * Show the form for creating a new resource. */ public function create() { return view('users.create'); } /** * Store a newly created resource. */ public function store(StoreUserRequest $request) { $result = $this->user->store($request->validated()); if ($result->status) { return redirect()->route($result->redirect_to)->with('success', $result->message); } return back()->with('danger', $result->message); } /** * Show the form for editing the specified resource. */ public function edit(int $id) { $data = $this->user->find($id); return view('users.edit', compact('data')); } /** * Update the specified resource. */ public function update(UpdateUserRequest $request, int $id) { $result = $this->user->update($id, $request->validated()); if ($result->status) { return redirect()->route($result->redirect_to)->with('success', $result->message); } return back()->with('danger', $result->message); } /** * Remove the specified resource. */ public function destroy(int $id) { $result = $this->user->destroy($id); if ($result->status) { return redirect()->route($result->redirect_to)->with('success', $result->message); } return back()->with('danger', $result->message); } /** * Toggle status of the resource. */ public function statusUpdate(int $id) { $result = $this->user->statusUpdate($id); if ($result->status) { return redirect()->route($result->redirect_to ?? 'users.index') ->with('success', $result->message); } return back()->with('danger', $result->message); } /** * Search resource by filters. */ public function search(array $filters = [], int $limit = 10): LengthAwarePaginator { return $this->user->search($filters, $limit); } /** * Restore a soft-deleted resource. */ public function restore(int $id) { $result = $this->user->restore($id); if ($result->status) { return redirect()->route($result->redirect_to)->with('success', $result->message); } return back()->with('danger', $result->message); } }
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to open issues or submit pull requests. Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-26