How to Build a REST API With Laravel

Laravel lets you easily and quickly build RESTful APIs. This could be the back-end to a front-end web app, a data source for a mobile app, or a service for other apps or APIs.

There are a lot of moving pieces to coding a RESTful API, but Laravel makes it a lot easier. In this free course, you’ll learn everything you need to know to build RESTful APIs with Laravel.

What You’ll Learn

  • define data models and seeding database test data
  • handle basic GET requests 
  • transform data from the database into a consistent and conventional JSON format
  • create flexible and reusable filter syntax for users to query and filter data
  • handle and validate POST, PUT, and PATCH requests
  • implement bulk inserts for end users to quickly insert multiple entities with a single request 
  • protect your API endpoints with Laravel Sanctum to authenticate and authorize requests

Who Is This Free Course For?

  • complete beginners who want to be web developers
  • experienced developers who want to explore advanced topics
  • programming enthusiasts who enjoy learning something exciting

Follow Along, Learn by Doing

I encourage you to follow along with this course, and you’ll learn about all the most important features of Vue.js

To help, the Build a Rest API With Laravel Github repository contains the source code for each lesson and the completed sample project that was built throughout the course. 

1. Introduction

Watch video lesson [0:00:00] ↗

This course will teach you how to build a Rest API with Laravel. In this introductory lesson, you’ll take a look at the scope of the course. 

2. Getting Started

Creating the Project

Watch video lesson [0:01:38] ↗

In this lesson, you’ll go over the things you’ll need to follow along in this course. Follow along to set up your dev environment and create your project.

Here is a quick tutorial on how to set up your Laravel environment for Windows:

Designing and Seeding the Database

Watch video lesson [0:7:36] ↗

In this lesson, you’ll create the migrations, factories, and seed data for our database.

3. Providing Data

Versioning and Defining Routes

Watch video lesson [0:19:22] ↗

Good web APIs are versioned, so you need to implement some kind of versioning. You’ll also set up the routes for the first version of the API.

Transforming Database Data Into JSON

Watch video lesson [0:26:17] ↗

RESTful APIs typically provide data in JSON format using JSON naming conventions. In this lesson, you’ll use Resource classes to transform our data into conventional JSON.

Filtering Data

Watch video lesson [0:35:48] ↗

You need to provide the ability for clients to filter results. You’ll implement a customer filter in this lesson.

Filtering More Data

Watch video lesson [0:49:47] ↗

In this lesson, you generalize our filter functionality so that you can reuse it for any resource.

Including Related Data

Watch video lesson [0:58:49] ↗

It sometimes makes sense to include related data in the API’s results, but it should be a feature that clients opt in to. You’ll implement that in this lesson.

4. Manipulating Data

Creating Resources With POST Requests

Watch video lesson [1:05:37] ↗

POST requests are for creating resources. You’ll handle POST requests to create customer resources in this lesson.

See the Laravel documentation on Built-in Validation Rules for more information.

Updating With PUT and PATCH

Watch video lesson [1:14:48] ↗

We update data using PUT and PATCH requests, and you’ll learn how to handle both types of requests in this lesson.

Implementing Bulk Insert

Watch video lesson [1:22:51] ↗

Some resources need to be inserted in bulk. You’ll learn how to implement that in this lesson.

5. Authentication

Protecting Routes With Sanctum

Watch video lesson [1:33:41] ↗

Laravel 7 introduced Sanctum, a token authentication scheme for APIs and SPA. It’s awesome, and you’ll use it to create tokens in this lesson.

Learn more about Laravel Sanctum in the official docs.

Authorizing Requests With Token Abilities

Watch video lesson [1:41:29] ↗

Sanctum allows us to assign abilities to tokens, and it’s easy to authorize requests. However, there’s a caveat that you’ll learn about in this lesson.

Conclusion

Watch video lesson [1:48:10] ↗

Laravel has the tools we need to build usable and scalable applications—including RESTful APIs. In fact, Laravel does a lot of the hard stuff for us, making it a much more enjoyable way to build software.

Learn more in the official Laravel Documentation or The Laravel Community Portal.

PHP Tutorial for Beginners – Full Course | OVER 7 HOURS!

Learn the fundamentals of PHP and object-oriented programming in this free 7-hour PHP tutorial. Jeremy McPeak will help you learn PHP and use it to write web apps. 

Leave a comment

Your email address will not be published.