Codementor Events

Abstracted CRUD Pattern in Laravel

Published Sep 26, 2018
Abstracted CRUD Pattern in Laravel

It’s pretty usual in developing an app that it always has lots of CRUD operations. The question is how do you tackle this? Let me show you an abstracted way of doing CRUD in laravel.

Imagine we are developing an e-commerce application, then we have the following modules, Users, and of course Products. It can have more modules but we can focus on these two.

So usually the first thing we do is identify the models and their attributes and prepare their table migrations.

The next thing we need to do is to visualize “How do those crud operations work? Do they have something in common?”. We all know that laravel gives us some convention on resource controllers for a typical crud operation. Let’s say we are creating the REST api backend for SPA application. So we will be working on these following methods: index(), store(), show($id), update($id), and destroy($id).

From the class diagram above, I can tell you that our users and products controllers are already done! But how will it look like in our code?

We will now create our base crud class. The code actually depends on what the application needs to do. For simplicity’s sake, I will just use minimal code to give you a demo and we will just use our models directly inside our controller, but in actual, you may want to use repositories.

The index()method.

Our index() method is as simple as that. It just returns a paginated list of our models of users and products. You can see that our BaseCrudController requires a model to be injected so we need to inject or models on bothUsersController and ProductsController. Since our UsersController and ProductsController extends to BaseCrudController, the index() method will be also inherited by the two controllers.

The create() method

For create() method, user and product models have different attributes so by having this abstract method inputStore(), it will be the one responsible for returning the user’s and product’s attributes to be stored.

For simplicity’s sake of this demonstration, I will not show how the validation code will look like, and try to figure out yourself how will you code your validation with this approach.

The update() method

The update() method code is self-explanatory. You can see that it is quite similar to the create() method since it also needs to return what attributes are needed to be updated via inputUpdate() method.

The show() and destroy() methods

The show() and destroy() methods are also self-explanatory and also automatically inherited by UsersController and ProductsController.

Conclusion

The key to achieve this is to identify the same behaviors and patterns so that you can put it in an abstract class and similar concrete classes can just inherit them. This helps to keep our code DRY and help us develop our application more efficient and faster.

Happy Coding!!! 😃

Discover and read more posts from Arjon Jason Castro
get started
post commentsBe the first to share your opinion
malikahfaz
a year ago

defining routes would be more helpful.

Show more replies