@@ -59,187 +59,6 @@ Add following code in root blade file in before close the body.
5959 }
6060 < / script>
6161```
62- # Repository Generator
63-
64- ## Features
65- With this package you can generate repositories with the ``` artisan make:repository ``` command.
66- The generator will generate the repository, repository interface and will bind them automatically (can be changed to
67- manual binding) to the Service Container so you can inject the interface into your controllers.
68-
69- ## Usage
70- For usage take the following steps. Generate the repository and then inject it into a controller or service.
71-
72- ### Generating repositories
73- Run the following command.
74- ```
75- php artisan make:repository UserRepository
76- ```
77- This example will generate the following files:
78- ```
79- app\Repositories\Eloquent\UserRepository
80- app\Repositories\UserRepositoryInterface
81- ```
82- # Pivot Table Generator
83-
84- ## Usage
85- Run the following command on the command-line to generate a new migration for the pivot table.
86- ```
87- php artisan make:pivot {first_table_name} {second_table_name}
88- ```
89-
90- The command will create a new migration in ``` database/migrations ``` . Run the migrations to create the table.
91- ```
92- php artisan migrate
93- ```
94-
95- # Service Generator
96-
97- ## Usage
98- After installation the ``` php artisan make:service {name} ``` will be available in the list
99- of artisan commands.
100-
101- ### Generate Service
102- To generate a new service use the following artisan command.
103- ``` bash
104- php artisan make:service UserService
105- ```
106-
107- ### Generate a service for a model
108- Add a ``` --service ``` or ``` -S ``` param to generate a service for the model.
109- ``` bash
110- php artisan make:model Post --service
111- ```
112-
113- Use the ``` -a ``` or ``` --all ``` param to generate a service, migration, seeder, factory, policy,
114- and resource controller for the model.
115- ``` bash
116- php artisan make:model Post --all
117- ```
118-
119- ### Generate a service for a controller
120- Add a ``` --service ``` or ``` -S ``` param to generate a service for the controller.
121-
122- ``` bash
123- php artisan make:controller PostController --service
124- ```
125-
126- ## The service pattern
127-
128- ### When to use the service pattern
129- A common question is: where do I put my business logic? You want to keep your models thin and your controller functions
130- skinny. There are multiple ways to archive this, extracting your business logic to the
131- service layer is a common method. By encapsulating your business logic in a service class you
132- are able to re-use the logic for example in your controllers, commands, jobs and middelware.
133-
134- ### How to use services
135- Once you have made a service it is time to add your business logic. We will discus how to use a service via static methods,
136- dependency injection and how to use it with interfaces and repositories.
137-
138- #### Static methods
139- a common way to use a service is to call it's methods statically. It is similar to helper functions. Let's say we have
140- a ``` PostService ``` with a method to get a post based on a slug.
141-
142- ``` php
143- namespace App\Services;
144-
145- use App\Models\Post;
146-
147- class PostService
148- {
149- // Declare the function as static
150- public static function getPostBySlug(string $slug): Post
151- {
152- return Post::with('tags')
153- ->where('slug', $slug)
154- ->get();
155- }
156- }
157- ```
158-
159- Next you can include the service class for example your controller and call the ``` getPostBySlug ``` method statically.
160- ``` php
161- namespace App\Http\Controllers;
162-
163- // Include the service
164- use App\Services\PostService;
165-
166- class PostController extends Controller
167- {
168- public function show(string $slug)
169- {
170- // Call the method statically from the service class
171- $post = PostService::getPostBySlug($slug);
172-
173- return view('posts.show', compact('post'));
174- }
175- }#
176- ```
177-
178- The ``` getPostBySlug ``` method is in this example a very simple function but as you can see it keeps you controller skinny
179- and and your business logic seperated. Keep in mind that static classes and methods are stateless. The class won't save
180- any data in itself.
181-
182- #### Dependency Injection
183- Another popular method is to use services with dependency injection. With dependency injection you can write loosely
184- coupled code. When done right this will improve the flexibility and maintainability of your code.
185-
186- The ``` PostService ``` we used as example before will remain
187- almost the same except we don't declare the functions inside the class as static anymore.
188-
189- ``` php
190- namespace App\Services;
191-
192- use App\Models\Post;
193-
194- class PostService
195- {
196- public function getPostBySlug(string $slug): Post
197- {
198- return Post::with('tags')
199- ->where('slug', $slug)
200- ->get();
201- }
202- }
203- ```
204-
205- Next we inject the service into the constructor of the class where we want to use it. Inside the constructor we
206- assign the object to the ``` $postService ``` class property. Now the ``` $postService ``` property will be callable in
207- all functions within the class with ``` $this->postService ``` . While typing your IDE will already typehint the functions
208- in your PostService class, in this case only ``` ->getPostBySlug($slug) ``` .
209- ``` php
210- namespace App\Http\Controllers;
211-
212- // Include the service
213- use App\Services\PostService;
214-
215- class PostController extends Controller
216- {
217- // Declare the property
218- protected $postService;
219-
220- // Inject the service into the constructor
221- public function __construct(PostService $postService)
222- {
223- // Assign the service instance to the class property
224- $this->postService = $postService;
225- }
226-
227- public function show($slug)
228- {
229- // Call the method you need from the service via the class property
230- $post = $this->postService->getPostBySlug($slug);
231-
232- return view('posts.show', compact('post'));
233- }
234- }
235- ```
236- # Action Generator
237-
238- ## Usage
239- Run the following command on the command-line to generate a new action.
240- ```
241- php artisan make:action {name}
242- ```
24362
24463## Contributing
24564
0 commit comments