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.



Now run above migrations with below command, it will create your tables as per migration file within 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 create an area
PUT – http://localhost:8000/areas/1 – you can update 1st area, here 1 is the primary key for the areas table.
DELETE – http://localhost:8000/areas/1 – you can DELETE 1st area, here 1 is the primary key for 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.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments Protection Status
Would love your thoughts, please comment.x