MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Create REST API in Laravel.

API is also known as Web services. Web services is very important when you are creating web and Mobile app. You require API in mobile application development. If you Don’t know how to create REST API in laravel then just follow the below Steps, i’m explaining everything step-by-step.  

Step 1: Install Laravel

In the first step, we require to get fresh Laravel application using below command, So open your terminal OR command prompt and run below command:

composer create-project --prefer-dist laravel/laravel laravel_api "5.4.*"Code language: JavaScript (javascript)

Step 2: Make Database Configuration

Laravel .env database settings: mysql on 127.0.0.1 port 3306, database laravel_api, user root

Step 3: Make Authentication scaffold

Make Laravel Authentication scaffold by below command and migrate

1. php artisan make:auth

2. php artisan make:authCode language: CSS (css)

Step 4: Add Dummy Data in Table.

Add some dummy data in users table so that when we call our API then it return the dummy data.

Step 5: Create the Controller.

Now, lets make a Controller in which we call country table to call the data from table. first, create a directory named API and under api create a Controller and put the below code.

<?php

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\User;

class apicontroller extends Controller
{
   
   public function index()
    {
        return User::all();
    }
}
Code language: HTML, XML (xml)

See the below image of Controller.

Apicontroller.php with index method returning User::all() in the Api namespace

Step 6: Create API Route.

Make api route to call our API. Go to routes/api.php and put the below code.

Route::get('countries', 'api\apicontroller@index');Code language: PHP (php)

See the below Image

Routes/api.php in Laravel project with Route::get countries pointing to apicontroller@index highlighted

Step 7: Call API Route.

Now we call the API route to access our Data. API URl is like

www.domainname.com/api/your_api_route.

I’m running my project in local so my url is http://localhost/laravel_api/public/api/countries

Open the API url to access the data. See the Below Image

Browser at localhost/laravel_api/public/api/countries showing JSON user records returned by the API

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

How to Create a Simple Login Authentication System in LARAVEL?Part-1

How to Create a Simple Login Authentication System in LARAVEL? Firstly, we’ll create a new project for this. Step 1. Go to XAMPP/htdocs folder -> right Click->Open Git…

Read More

Top 10 Laravel Interview Questions And Answers 2020.

1- What is Laravel Framework? Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller…

Read More

How to use multiple authentication in Laravel.

Step:1 Step:2- click on Git base Here. It will look like this Step:3- you have to crate own project and put here Step:4- Press Enter than project…

Read More

How to Upload files or Documents in Mysql Database Using laravel.

Step: 1- First of all you have to go:- Step:2- then right-click in this Drive and got to Git Bas Here and click. Step:3-run the composer step:…

Read More

Todo Application in Laravel

1.Create Project in Laravel Composer create-project laravel/laravel todolist 2.Create CRUD Controller php artisan make:controller TodosController –resource 3.Route setup of CRUD Controller routes/web.php 4.Create Database todolist localhost/phpmyadmin create…

Read More

Email API: Generating and Delivering Templates Programmatically

An email API for templates lets applications create, edit, and export HTML email content programmatically instead of coding every message manually. It separates template design and generation…

Read More