Create a full crud API quickly in Laravel in (HMVC) pattern! (part 1)
- What is Laragine?
- STEP 1: Install Laravel 8
- STEP 2: Database setup
- STEP 3:Require Laragine package
- STEP 4: Install the package
- STEP 5: Create a blog module
- STEP 6: Create unit
- STEP 7: Add API route
- STEP 8: Testing
- Youtube suggestion
- Unit-test (part2)
There are different types of APIs, but today we are going to concentrate on RESTful APIs. REST stands for Representational State Transfer, while API stands for Application Programming Interface. You can read more about API from the internet or other programming material.
We’ll use a Laravel package called Laragine, this package helps us to create a fully CRUD system by just writing simple commands Without saying much, let’s dive into it.
What is Laragine?
Laragine is a Laravel package that was created to manage your large Laravel app using modules, let’s say that you’ll build a blog the blog consists of (post — comment — like), blog-> is a module, (post - comment - like) -> called units Laragine built the module & units with all CRUD functionalities also built (Migrations - factories - tests - controllers - models) all you have to do is to just include the link in route files, awesome, right!
In this tutorial, we’ll just create a post for a blog with fully CRUD functionalities so we’ll create a module called blog then we’ll create a unit called post, might be you’re asking now why we will build the application in a modular way? if you don’t know please see this article that I created talks about how we can organize big projects in Laravel.
STEP 1: Install Laravel 8
To install the latest Laravel framework, which is Laravel 8.0 as of the time of publishing this article, run the command below
composer create-project --prefer-dist laravel/laravel laravel_8
This will automatically create a Laravel 8 app and a couple of things have been set up, we don’t need to copy and rename the env.example file, Laravel 8 does that automatically for us.
Another important thing about Laravel 8, you don’t need to generate APP_KEY, this new version will also generate it for us.
With that all set up, our app is ready.
STEP 2: Database setup
Create an empty database, Open the .env file, and update your database configurations.
STEP 3: Require Laragine package
Let us install Laragine.
composer require yepwoo/laragine
STEP 4: Install the package
After including Laragine, you have to install it by running the following command:
php artisan laragine:install
it's an initial command for Laragine
After running the command, the Laragine directory will be in the root directory under
If you want to know more information about the package visit the documentation
STEP 5: Create a blog module
Run the following command and specify the name of the module in our case will be
php artisan laragin:module blog
Any module will be added under
After running the command, the blog directory will be in the
STEP 6: Create unit
After creating our module (
blog) we need to create a
The unit command consists of 2 commands
Creates all basic files and will create a JSON file that we'll specify the attributes that we want to add it to the database, we should specify the name of the module that we want to add the unit under it, which is
blog in this example.
it’s the same command without the init option, the command will automatically create all the following files depending on the data that we specified in the
- unit tests
let’s run the commands, we should specify
name of the module that we want to create the unit under it & the
name of unit
php artisan laragin:unit post --module=blog --init
--init because it's the first part of unit command
After running the command it'll generate the basic files
Controller (API - web) - Model & also the
JSON file that we'll write our attributes in it.
Now we should specify our attributes in the
Post.json file, let's first know what the attributes that we need! let's start with the basics in this article I just want to show you how to use the package, you can add any amount of attributes you want, any post have
title, body, thumbnail
if we don’t put the attribute as
nullable will be
required by default
- title -> type:
- body -> type:
- image -> type:
Now let’s go to the JSON file which is in
core/blog/data/Post.json to write our attributes.
we should follow
rules when writing any attribute in
JSON file, there are two types of configuration
type & definition
type:is the type of attribute you should write it as
Laravelspecified in docs you can check all types in Laravel from here
definition:is for column modifier also you should write it as Laravel specified in the docs you can check it from here
if you realized that I didn’t write
required because the package will add it automatically.
Let’s run the second part of
php artisan laragin:unit post --module=blog
After running the command it’ll generate
migrations - requests - resources - factories - unit tests - etc... files and they are fully implemented.
let’s see the
migration file, you'll see all
attributes are added automatically with the configuration that we specified in
JSON file, magic, right🧐!
- Now let’s run
php artisan migrate
STEP 7: Add API route
Route::apiResource('posts', 'PostController'); in
- we don’t need to implement
CRUDmethods, it's already implemented in
STEP 8: Testing
Postmanor any other software you use to test the
- don’t forget to put in headers
- let’s first create a new post
- let’s create another post but without including the
titleto just test if it'll return
- Now you’re wondering how it gives us the
titleattribute is required, right 🤔? let's see
core/Blog/Requests, all attributes that we specified are in the file with some
Laraginedoes this job for us.
you can modify any attribute you want in
request file or in any other file
Resources - unit test - etc...
- Let’s get all posts
- Let’s update the post
before testing the update method, you may want to change the
validationin the update request (put) and you can do this from
- Let’s delete the post
in this article, I just showed you
one feature of Laragine package, in the next tutorial I'll teach you how to use
unit tests by just making a small configuration.
And that brings us to the end! Thank you so much for reading through this - if this article was helpful in any sense I’d really appreciate a follow to help me reach my 100-follower goal by the end of May 🙂.
If you want to make videos on Youtube for better clarification please write in a comment, it’ll encourage me to make videos.
Check part 2
Create a full crud API in Laravel — unit tests part (2), check it.