Posted on Arjun

Build REST apis with Laravel 4.2

Create a brand new Laravel Project with below command

Configure Your Database

Open config/database.php file and update it with your database details as shown below

Create Migration files and tables

We gonna have users and areas tables, so lets create migration file with below commands, which will create creates new files in your app/database/migration directory

Let’s updated your migration file’s up methods with below code blocks.

2017_10_04_085548_create_users_table.php

2017_10_04_085555_create_areas_table.php

Now run above migrations with below command, it will create your tables as per migration file with in the specified database(app/config/database.php).

Create Models

Let’s create user and area models as shown below.
create User.php file in app/modes directory with below code

create Area.php file in app/modes directory with below code

Create Controller

Create AreaController.php file in your app/controller directory with below code

Define route

Open your app/routes.php file and add following route definition.

Now you can access following routes –
GET – http://localhost:8000/areas – it will return all the areas
POST – http://localhost:8000/areas – you can crate a area
PUT – http://localhost:8000/areas/1 – you can update 1st area, here 1 is primary key of the areas table.
DELETE – http://localhost:8000/areas/1 – you can DELETE 1st area, here 1 is primary key of the areas table.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status