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

Buying from the shop
  • An IDE, preferably PHPStorm or Visual Studio Code.
  • Lumen micro-framework a “lightweight laravel”
  • PostgreSQL database
  • Postico a database management tool for mac
  • PHP installed
  • Postman (Rest client for testing the API)
  • With composer installed on your computer.
  • Now open up your terminal and run the following command.
  • composer create-project laravel/lumen Shopper
  • In the above command, Shopper is the name of your project. You will find a folder with the same name in the directory where you ran the above command.
  • There are very many folders here but the most important ones for our application are
  • app: Here we will create a Models folder that will house our models that will act as a means for communicating or interfacing with the database easily.
  • app/http/controllers: This is a folder that will be hosting our controllers for Shopper.
  • app/http/middlewares: We will define the API security here.
  • migrations: This is where we will define the files that will represent our database schema. These files are called migrations and act kinda like version control for our tables.
  • routes: This is where endpoints for Shoppers API will be defined.
  • Create a database called shoppers in postico for Mac users or any other database administration tool for other platforms.
  • You do not need to create any tables. Just create a database. (I have created a database named shoppers).
  • Now within your project folder in your IDE open .env file, and modify it as below.
  • Next, we will create a database schema in migrations.
  • Open your terminal or cmd at the root of your project
  • 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
  • Next run
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.
  • The new migration will be placed in database/migrations directory. For more info about migration visit Lumen/Laravel documentation.
  • Now our database schema is ready. Now, let's run the migrations and create table in our Postgres database.
  • In the terminal type php artisan migrate. And the database will be populated with four tables namely migration, users, categories and items table automatically.
  • Now check postico and you will see that the tables are created. See image below
Results after running migrations
  • 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.
  • Eloquent is not enabled in Lumen by default so, head over to your bootstrap/app.php file and uncomment the lines
  • $app->withEloquent();
  • $app->withFacades();
  • Now create a folder called Models in your app folder
  • Finally create User.php, Category.php, and Item.php inside the Models folder.
  • Now within routes/web.php add the following code.
  • Now create all controllers inside app/http/controllers.
  • All controllers extend the class Controller.
  • Now do composer require illuminate/routing
composer require firebase/php-jwt
app->routeMiddleware([‘jwt.auth’ => App\Http\Middleware\JwtMiddleware::class,]);

--

--

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
Adrian Sebuliba

Adrian Sebuliba

I’m a Software Engineer in love with everything tech