Laravel Lumen CRUD REST API with Angular ShoppingList Application -part 1

Buying from the shop


Before we get started, I assume you are a familiar with Lumen or Laravel and you just want to get the complete workflow of how to build a project that fully covers Create Read Update Delete (CRUD) plus JWT authentication in laravel lumen and that you want to understand how the whole thing (REST API) can be consumed with a client application written in say Angular or any other client written in Javascript library or framework.

In this part 1 of the two-part series of creating a shopping list application called shoppers, I will build a REST API in Lumen a PHP microframework from the same guys who brought to you laravel.

Before we get started, ensure that you have the following tools already installed on your local machine.

  • An IDE, preferably PHPStorm or Visual Studio Code.

Now let's get started.

Database Design:

Let's begin with creating the project

Creating Lumen Project

  • With composer installed on your computer.

Now open Shopper in your favorite IDE preferably PHPStorm

Folder Structure

  • There are very many folders here but the most important ones for our application are

Setting Up Database:

For the database, we will

  • Create a database called shoppers in postico for Mac users or any other database administration tool for other platforms.

You are free to change the above .env according to your database configuration. The above file is only shown here for demo purposes.

  • Next, we will create a database schema in migrations.

Database Migrations

Migrations are blueprints for tables to be created or modified in a database.

Simple migrations can be created by using the command

php artisan make:migration <name_of_migration> when creating new tables. However, as time goes by there will arise the need to modify certain table(s) in our database which will involve creating new migration(s) to modify the said table(s)

  • Open your terminal or cmd at the root of your project

Creating Migrations

  • On the terminal and issue the following command.
php artisan make:migration create_users_table --create=users
  • It is very important that you begin by creating the users table schema because the other tables will be heavily dependant on it. Creating this schema first will save us future problems when running migrations
php artisan make:migration create_categories_table --create=categoriesphp artisan make:migration create_items_table --create=items
  • In that order. Notice how categories migrating came before items migrations. This ensures that you do not run into problems while running migrations if you happen to define foreign key constraints.

Designing Database Schema

Now populate the above-created schemas as shown below:




  • Now our database schema is ready. Now, let's run the migrations and create table in our Postgres database.

Making Migration:

  • In the terminal type php artisan migrate. And the database will be populated with four tables namely migration, users, categories and items table automatically.
Results after running migrations

Making Models

  • Models make interacting with the database a breeze. The Laravel Eloquent ORM will be used with no single line of SQL queries written to interact with the database.




Creating URL EndPoints

  • Now within routes/web.php add the following code.


  • Now create all controllers inside app/http/controllers.




Adding JWT Authentication:

For JWT authentication we are going to use the firebase/php-jwt library so

Run the following composer command in the terminal:

composer require firebase/php-jwt

Create a file JwtMiddleware.php under the middleware folder

Now in your bootstrap/app.php under register middleware add this line of code to register above middleware

app->routeMiddleware([‘jwt.auth’ => App\Http\Middleware\JwtMiddleware::class,]);

Now that everything is set up, we are ready to test our application in postman.

For details on how to set up everything and get started, go to the Github repo.

Thanks for reading :).

Part 2 of this series can be found here.



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