|
1 | 1 | <p> |
2 | 2 | <a href="https://packagist.org/packages/arafat69/laravel-repository"> |
3 | | -<img alt="Packagist Stars" src="https://img.shields.io/packagist/stars/arafat69/laravel-repository"> |
| 3 | + <img alt="Packagist Stars" src="https://img.shields.io/packagist/stars/arafat69/laravel-repository"> |
4 | 4 | </a> |
5 | | -<a href="https://packagist.org/packages/arafat69/laravel-repository"> |
| 5 | +<a href="https://github.com/arafat69/laravel-repository/issues"> |
6 | 6 | <img alt="GitHub issues" src="https://img.shields.io/github/issues/arafat69/laravel-repository"> |
7 | 7 | </a> |
8 | | -<a href="https://packagist.org/packages/arafat-dev/laravel-repository"><img src="https://img.shields.io/packagist/dt/arafat69/laravel-repository" alt="Total Downloads"></a> |
9 | | -<a href="https://packagist.org/packages/arafat-dev/laravel-repository"><img src="https://img.shields.io/packagist/v/arafat69/laravel-repository" alt="Latest Stable Version"></a> |
10 | | -<a href="https://packagist.org/packages/arafat-dev/laravel-repository"><img src="https://img.shields.io/packagist/l/arafat69/laravel-repository" alt="License"></a> |
| 8 | +<a href="https://packagist.org/packages/arafat69/laravel-repository"> |
| 9 | + <img src="https://img.shields.io/packagist/dt/arafat69/laravel-repository" alt="Total Downloads"> |
| 10 | +</a> |
| 11 | +<a href="https://packagist.org/packages/arafat69/laravel-repository"> |
| 12 | + <img src="https://img.shields.io/packagist/v/arafat69/laravel-repository" alt="Latest Stable Version"> |
| 13 | +</a> |
| 14 | +<a href="https://packagist.org/packages/arafat69/laravel-repository"> |
| 15 | + <img src="https://img.shields.io/packagist/l/arafat69/laravel-repository" alt="License"> |
| 16 | +</a> |
11 | 17 | </p> |
12 | 18 |
|
13 | | -# Laravel-Repository |
14 | | -Simple but powerfull laravel repository pattern |
| 19 | +# Laravel Repository |
| 20 | + |
| 21 | +A simple, lightweight, and powerful Laravel repository pattern package with built-in **Artisan commands** for generating repositories quickly. |
| 22 | +Supports **Laravel 7 and above**. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Features |
| 27 | + |
| 28 | +- Generate repositories using Artisan commands |
| 29 | +- Supports **model-specific repositories** |
| 30 | +- Clean, maintainable code following **repository pattern** |
| 31 | +- Easy CRUD operations in repositories |
| 32 | +- Supports all Laravel versions from **7+** |
| 33 | +- Optional stubs publishing for customization |
15 | 34 |
|
16 | 35 | --- |
17 | 36 |
|
18 | 37 | ## Installation |
19 | 38 |
|
20 | | -```sh |
| 39 | +```bash |
21 | 40 | composer require arafat69/laravel-repository |
22 | 41 | ``` |
23 | 42 |
|
24 | | -After installing the package you will see a file **repository.php** is created inside **repositories** folder |
| 43 | +After installing, a repository.php file will be created in your Repositories folder. |
25 | 44 |
|
26 | | -##Make a repository |
| 45 | +--- |
27 | 46 |
|
28 | | -```php |
| 47 | +## Generating a Repository |
| 48 | + |
| 49 | +```bash |
29 | 50 | php artisan make:repository UserRepository |
30 | | -or |
| 51 | +``` |
| 52 | + |
| 53 | +With a model: |
31 | 54 | // use scope for specific model |
| 55 | + |
| 56 | +```bash |
32 | 57 | php artisan make:repository UserRepository --model=User |
33 | 58 | ``` |
34 | 59 |
|
35 | | -## How to create function in repository |
| 60 | +--- |
| 61 | + |
| 62 | +## Defining Functions in Repository |
36 | 63 |
|
37 | 64 | ```php |
38 | | -//Create |
| 65 | + |
| 66 | +// Create a new record |
39 | 67 | public static function storeByRequest($request): User |
40 | 68 | { |
41 | 69 | return self::create([ |
42 | 70 | 'first_name' => $request->first_name, |
43 | 71 | 'last_name' => $request->last_name, |
44 | 72 | 'email' => $request->email, |
45 | | - // ... |
| 73 | + 'phone' => $request->phone, |
| 74 | + //... |
46 | 75 | ]); |
47 | 76 | } |
48 | 77 |
|
49 | | -// Update |
| 78 | +// Update existing record |
50 | 79 | public static function updateByRequest($request, User $user): User |
51 | 80 | { |
52 | | - self::update($user, [ |
| 81 | + $user->update([ |
53 | 82 | 'first_name' => $request->first_name, |
54 | 83 | 'last_name' => $request->last_name, |
55 | 84 | 'email' => $request->email, |
56 | | - // ... |
| 85 | + 'phone' => $request->phone, |
| 86 | + //... |
57 | 87 | ]); |
58 | 88 |
|
59 | | - // or |
60 | | - |
61 | | - $user->update([ |
62 | | - // your update data |
63 | | - ]) |
64 | | - |
65 | 89 | return $user; |
66 | 90 | } |
67 | | -// etc... |
| 91 | + |
68 | 92 | ``` |
69 | | -## Use from controller |
70 | 93 |
|
71 | | -#### Import first the Repository |
| 94 | +--- |
| 95 | + |
| 96 | +## Repository Function Examples |
72 | 97 |
|
73 | 98 | ```php |
74 | | -//example UserRepository |
75 | | -use App\Repositories\UserRepository; |
| 99 | + |
| 100 | +namespace App\Repositories; |
| 101 | + |
| 102 | +use App\Models\User; |
| 103 | +use Illuminate\Http\Request; |
| 104 | +use Arafat\LaravelRepository\Repository; |
| 105 | + |
| 106 | +class UserRepository extends Repository |
| 107 | +{ |
| 108 | + /** |
| 109 | + * base method |
| 110 | + * |
| 111 | + * @method model() |
| 112 | + */ |
| 113 | + public static function model() |
| 114 | + { |
| 115 | + return User::class; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Create a new record from request |
| 120 | + */ |
| 121 | + public static function storeByRequest(Request $request): User |
| 122 | + { |
| 123 | + return self::create([ |
| 124 | + 'first_name' => $request->first_name, |
| 125 | + 'last_name' => $request->last_name, |
| 126 | + 'email' => $request->email, |
| 127 | + 'phone' => $request->phone, |
| 128 | + // add other fields here |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Update an existing record |
| 134 | + */ |
| 135 | + public static function updateByRequest(Request $request, User $user): User |
| 136 | + { |
| 137 | + $user->update([ |
| 138 | + 'first_name' => $request->first_name, |
| 139 | + 'last_name' => $request->last_name, |
| 140 | + 'email' => $request->email, |
| 141 | + 'phone' => $request->phone, |
| 142 | + // add other fields here |
| 143 | + ]); |
| 144 | + |
| 145 | + return $user; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Update the logged-in user's profile |
| 150 | + */ |
| 151 | + public static function profileUpdate(Request $request): User |
| 152 | + { |
| 153 | + $user = auth()->user(); |
| 154 | + $user->update($request->only(['first_name', 'last_name', 'email', 'phone'])); |
| 155 | + return $user; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get order summary for a user |
| 160 | + */ |
| 161 | + public static function orderSummary(User $user) |
| 162 | + { |
| 163 | + return $user->orders() |
| 164 | + ->selectRaw('count(*) as total_orders, sum(total) as total_amount') |
| 165 | + ->first(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get all active users |
| 170 | + */ |
| 171 | + public static function getActiveUsers() |
| 172 | + { |
| 173 | + return self::query()->where('status', true)->get(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Delete a user |
| 178 | + */ |
| 179 | + public static function deleteUser(User $user): bool |
| 180 | + { |
| 181 | + return $user->delete(); |
| 182 | + } |
| 183 | +} |
| 184 | + |
76 | 185 | ``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## Using Repository in Controller |
| 190 | + |
77 | 191 | ```php |
78 | | -// get all user |
79 | | -UserRepository::getAll(); //retun all users |
80 | | -// filter user use query |
81 | | -UserRepository::query()->whereName('jon')->get(); |
82 | | -// store method call |
| 192 | + |
| 193 | +use App\Repositories\UserRepository; |
| 194 | + |
| 195 | + |
| 196 | +// --- Custom repository methods --- |
| 197 | + |
| 198 | +// Store user |
83 | 199 | UserRepository::storeByRequest($request); |
84 | | -// update method call |
85 | | -UserRepository::updateByRequest($request, $user); |
86 | | -//find |
87 | | -UserRepository::find($userID); |
88 | | -//get first recode |
89 | | -UserRepository::first(); |
90 | | -// delete recode |
| 200 | + |
| 201 | +// Update a user |
| 202 | +$user = UserRepository::updateByRequest($request, $user); |
| 203 | + |
| 204 | +// Update current logged-in user's profile |
| 205 | +$currentUser = UserRepository::profileUpdate($request); |
| 206 | + |
| 207 | +// Get order summary |
| 208 | +$orderSummary = UserRepository::orderSummary($user); |
| 209 | + |
| 210 | +// Get all active users |
| 211 | +$activeUsers = UserRepository::getActiveUsers(); |
| 212 | + |
| 213 | +// Delete a user |
| 214 | +UserRepository::deleteUser($user); |
| 215 | + |
| 216 | +// --- Built-in repository methods (from the base repository) --- |
| 217 | + |
| 218 | +// Get all users |
| 219 | +$users = UserRepository::getAll(); |
| 220 | + |
| 221 | +// Query users with conditions |
| 222 | +$users = UserRepository::query()->where('name','Jon')->get(); |
| 223 | + |
| 224 | +// Find a specific user by ID |
| 225 | +$user = UserRepository::find($userID); |
| 226 | + |
| 227 | +// Get the first record |
| 228 | +$user = UserRepository::first(); |
| 229 | + |
| 230 | +// Delete a user by ID |
91 | 231 | UserRepository::delete($userID); |
| 232 | + |
92 | 233 | ``` |
93 | | -#### Publish stubs folder |
94 | | -```sh |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +### Publishing Stubs |
| 238 | + |
| 239 | +```bash |
95 | 240 | php artisan vendor:publish --tag=stubs |
96 | 241 | ``` |
| 242 | + |
| 243 | +--- |
| 244 | + |
97 | 245 | ## Contribution |
98 | | -You're open to create any Pull request. |
| 246 | + |
| 247 | +Feel free to open Pull Requests or submit issues. |
| 248 | +Contributions are welcome! |
| 249 | + |
| 250 | +--- |
| 251 | + |
| 252 | +## License |
| 253 | + |
| 254 | +MIT |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +## Changelog v2.0.0 |
| 259 | + |
| 260 | +- Minimum PHP requirement updated to 7.3 |
| 261 | +- Laravel support updated to 7 and above |
| 262 | +- Artisan repository generator improved with interactive output |
| 263 | +- Composer keywords and description updated for better search visibility |
| 264 | +- Stubs publishing for customization added |
| 265 | +- Cleaned up examples and documentation |
0 commit comments