Laravel is a PHP framework developed with PHP developer productivity in mind. The framework also aims to evolve with the web and has already incorporated several new features and ideas in the web development world, such as job queues, API authentication out of the box, real-time communication, and much more.
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer.
REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.
Let's begin to create our fresh laravel application for RESTful API.
Let's configure database for our application. If you have don't have any database then create a new one. After creating database open .env file from root directory of your project. if .env file is missing from project then copy content from .env.example and create file. .env file defines many common environment variables
Replace * with your actual configuration in upper code.
Before creating model and migrations let's understand what is model and migrations.
In Laravel, Model is a class that represents the logical structure and relationship of underlying database table. In Laravel, each of the database table has a corresponding Model that allow us to interact with that table. Models gives you the way to retrieve, insert, and update information into your data table. All of the Laravel Models are stored in the main app directory.
Migrations are like version control for your database, allowing your team to define and share the application's database schema definition.
The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Typically, migrations will use this facade to create and modify database tables and columns.
Let’s get started with our first model and migration - the Books. The book should have a title and a author field, as well as a publish date. Laravel provides several commands through Artisan - Laravel’s command line tool that help us by generating files and putting them in the correct folders. To create the Books model, we can run:
This will create a Contact model and a migration file. In the terminal, we get an output similar to:
Open the app/models/Books.php file and update it accordingly:
Open the database/migrations/xxxxxx_create_books_table migration file and update it accordingly:
We added the name, author and publish date fields in the books table structure.
After defining our table structure, we have to create table into database using below command :
Database seeding is the process of filling up our database with dummy data that we can use to test it. Laravel comes with Faker, a great library for generating just the correct format of dummy data for us. So let’s create our first seeder:
The seeders will be located in the /database/seeds directory. Here’s how it looks like after we set it up to create a few books data:
So let’s run the seed our database using this command:
Controllers are meant to group associated request handling logic within a single class. In your Laravel project, they are stored in the app/Http/Controllers' directory. The full form of MVC is Model View Controller, which act as directing traffic among the Views and the Models. After creating the model and migrated our database. Let’s now create the controller for working with the Books model.
In your terminal, run the following command:
This command will generate a controller at app/Http/Controllers/BookController.php and update it accordingly :
Route is a way of creating a request URL of your application. These URL do not have to map to specific files on a website. The best thing about these URL is that they are both human readable and SEO friendly.
We have already create method to handle CRUD for books table. Now we need to provide implementations for these methods at specific URL. For that we have to create route for particular functions. In Laravel, All API routes are must be added into particular file routes/api.php.although you can create custom route file and bind it to your application.
In this example, we are going to use default route file so Open the routes/api.php file and update it accordingly
These route binds functions to specific URL which is listed below:
For Running or Testing API application enter below command :
It will produce output like below :
Now we can test our API endpoints using any API testing tool.
Ask anything about this examples