API with Laravel

Rasathurai Karan
2 min readJun 12, 2022

Small intro about Laravel

If you know PHP and oops concept , let’s take the laravel on your hands . The development of web applications and websites has become increasingly simple in recent years. So let’s jump to this article.

Let's get a concept of larval. Laravel is a PHP framework for building web applications. Laravel has a large library of pre-programmed functionality (such as authentication, routing, and HTML templating).This is the biggest advantage of using Laravel. It offers authentication and application starter kits. So you don’t need to manually write code for authentication.

Request for learning Laravel

  1. PHP
  2. OOP concepts
  3. HTML
  4. MVC Architecture

A small intro about API

“API” stands for “application programming interface,” and APIs are the unsung heroes of the modern web. From a technical perspective, an API is a set of programming instructions and standards for accessing a web-based software application or service.

API with Laravel

How to get data from database using API

initially create a laravel project. laravel new myWebApp.

then set up the database by using the suitable name and create it.

then create controller php artisan make:controller PostController.

php artisan make:model Post -m. through this command, we create a mode,l and also we can create migration for Post model.

$table ->String(‘title’);$table->text(‘content’);

add these fields inside the up function

then we want to migrate the changes into our database .so we need to cmd like php artisan migrate.

after that let's try to add some fake data to our database .so go to database/seeders/databaseseeders.php.basically, Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory

Faker generates fake or dummy data for your application and has built-in capability to generate fake data

then go to the database and check the post table .you can see the 10 dummy data are inserted.

then go to PostController

k

Post::all() we can get all post ,and assign to $post variable.

after that goto routes/api.php

Route::get('get-post'[PostController::class,'getPost'])`

then let's check or test the API by Postman before doing this run the server.

php artisan serve `

then add the URI api/get-post

continue…..

--

--

Rasathurai Karan

Department of Computer Engineering University of Peradeniya