Create a full crud API quickly in Laravel in (HMVC) pattern! (part 1)

Agenda

  • Introduction
  • Prerequisites
  • 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
  • Conclusion
  • Youtube suggestion
  • Unit-test (part2)

Introduction

Good day, today we are going to build a fully CRUD system (Web & API). API is a software intermediary that allows two applications to talk to each other. Somethings you might need to create an app that can be run on different languages or frameworks, for example, you can use Laravel to create the backend for your application while the frontend might run on any JavaScript frameworks. API allows two or more programs to communicate with each other.

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.

Prerequisites

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

Note

After running the command, the Laragine directory will be in the root directory under core directory.

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 blog
php artisan laragin:module blog

Any module will be added under core directory
After running the command, the blog directory will be in the
coredirectory.

STEP 6: Create unit

After creating our module (blog) we need to create a post, right?
The unit command consists of 2 commands
Init command:
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.

Second command:
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 JSON file.

  1. Migration
  2. Requests
  3. Resources
  4. Factories
  5. 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

We include --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

Note

if we don’t put the attribute as nullable will be required by default

Attributes:

  • title -> type: string, definition: unique
  • body -> type: text,
  • image -> type: string, definition: nullable

Now let’s go to the JSON file which is in core/blog/data/Post.json to write our attributes.

Note

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 Laravel specified 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 unit command

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 migrate command
    php artisan migrate

STEP 7: Add API route

add Route::apiResource('posts', 'PostController'); in api.php in core\Blog\routes\api.php

Note

  • we don’t need to implement CRUD methods, it's already implemented in PostController in core\Blog\Controllers\API\PostController.

STEP 8: Testing

  • open Postman or any other software you use to test the API
  • don’t forget to put in headers Accept: application/json
  • let’s first create a new post
  • let’s create another post but without including the title to just test if it'll return error or not
  • Now you’re wondering how it gives us the title attribute is required, right 🤔? let's see PostRequest file in core/Blog/Requests, all attributes that we specified are in the file with some validation, Laragine does this job for us.

note

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 validation in the update request (put) and you can do this from PostRequest in core/Blog/Requests file in put method
  • Let’s delete the post

Conclusion

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 🙂.

Youtube Suggestion

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.

Let’s connect on LinkedIn, Twitter

--

--

--

Software engineer, Problem solver, Geek.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What are the Benefits of an Open-Source Low-Code Platform?

10 days @ code societies SFPC

Ethereum mainnet and testnets now available for Amazon Web Services users

The Encyclopedia of Online Coding Platforms — a killer new book that’s FREE (or darn near)

Learn AWS Lambda in 5 minutes

All about Mobile Development in 2020

Advantages and Disadvantages of Using AWS

Great click-bait.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdlrahmansaber

Abdlrahmansaber

Software engineer, Problem solver, Geek.

More from Medium

Build a WhatsApp Chatbot using PHP

Build a WhatsApp Chatbot using PHP — ultramsg

UPDATED: How to Send Webhook in Laravel

Laravel MySQL index hints scope

Top 5 Best PHP Frameworks to use in 2022

5 Best PHP Frameworks