1. Intro & Setup
Welcome to the Laravel API course! Get ready to dive into the exciting world of building APIs with Laravel, the powerful and flexible PHP framework.
15:57
2. Cleaning up Laravel
Ready to supercharge your Laravel project for an API-first approach? Let’s dive into optimizing and cleaning it up to make it lightning-fast and ultra-efficient!
16:19
3. Data Modeling
When building an API, mastering the art of structuring your data and defining dynamic relationships between various entities is absolutely essential in Laravel 11.
24:27
4. API Design
When you design an API, it’s absolutely crucial to understand that every API should have a meaningful purpose!
33:09
5. Authentication & Authorization
Say goodbye to the hassle of manually crafting every single detail, and say hello to a streamlined, efficient process that keeps your endpoints vividly documented and ready to go!
14:15
6. Rate Limiting
When it comes to managing API requests, rate limiting is a key player. You don’t want to open the floodgates and let users bombard your API with endless requests.
8:24
7. API Versioning
API versioning might sound like a minor detail at first, but trust me, it's one of those decisions that can make or break the future usability of your API.
22:22
8. Pagination
When you're handling requests that could return extensive lists of resources, it's essential to implement pagination to ensure your responses are manageable and user-friendly.
11:43
9. API Standards
API standards, while useful, aren’t the be-all and end-all. What often takes precedence is discoverability and consistency in your API design.
26:17
10. Handling Errors
Today, we'll explore how to leverage Laravel 11’s error handling system by implementing the API Problem Specification and enhancing it with custom packages to provide consistent, informative error responses.
26:49
11. Write Operations
Let’s explore the steps to manage resources in a Laravel 11 API, focusing on creating, validating, updating, and deleting resources. We’ll use a real-world approach to ensure clarity and usability.
29:30
12. Internationalization
In our previous video, we dove deep into handling write operations—everything from data validation and authorization to deciding between synchronous and asynchronous processing.
11:44
13. Caching Data
In our previous video, we dove deep into handling write operations—everything from data validation and authorization to deciding between synchronous and asynchronous processing.
18:08
14. Basic Security
When it comes to API security, think of it not as a threat but as an opportunity—a chance to build robust, multilayered defenses around your API.
9:53
15. Cache Requests
In Laravel 11, we can take advantage of built-in HTTP features to manage our cache more effectively, streamlining both the response time and the overall user experience.
8:40
16. Scheduling tasks & Sending requests
When working on a Laravel API, the task scheduling system plays a significant role in automating background jobs.
40:33
17. Notifications
Whether you're building a ping service or any application that requires user communication, you’ll likely need to notify users when certain actions occur, such as a service failure.
15:57
18. Monitoring and Logging
When you're developing APIs, it's important to have a clear picture of how your API is performing in real-time.
21:33
19. Testing
When you're developing APIs or any web-based product using Laravel 11, testing plays a crucial role in ensuring that your application behaves as expected.
18:01
20. API Platform
When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications.
17:16
21. Feature Flags
When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications.
11:05
22. Web Sockets
When you're working with APIs, there's a common misconception that WebSockets aren't relevant.
9:58
23. Search API
When you're building APIs, one key feature that often gets overlooked is search. That's what we’ll explore today using TypeSense(https://typesense.org/), a powerful open-source search engine.
15:30
24. Documenting your API
By default, API Platform will give you an OpenAPI specification (currently version 3.1). This standard format allows your API to be easily understood by machines and developers alike.
07:06
API Standards
When learning API development, you might encounter various API standards. Some developers swear by them, while others might overlook their importance. Here’s a candid take: API standards, while useful, aren’t the be-all and end-all. What often takes precedence is discoverability and consistency in your API design. In this guide, we’ll explore different API standards, focusing on how to effectively use Laravel’s API resources to create robust and user-friendly APIs. We'll also learn best practices and real-world examples to ensure you can transform your data seamlessly for any client, whether it’s an end-user or another service.
Crafting API Resources in Laravel 11
In Laravel 11, the process of creating API resources remains a cornerstone for efficient and scalable API development. API resources give you control over how your data is transformed and served to the client, which becomes even more important as your application grows.
Why Use API Resources?
API resources in Laravel allow you to format and transform your data before sending it back to the client. Rather than exposing raw database structures, you gain flexibility in how your API responds to requests. Whether you're managing complex relationships or simply customizing the JSON structure, resources ensure that your API responses are clean and standardized.
Creating a Service Resource
To get started, let’s create a resource for a service in Laravel 11. Open your terminal and run the following command:
php artisan make:resource V1/ServiceResource
This command generates a resource class extending JsonResource
. Here’s a basic example:
namespace App\Http\Resources\V1;
use Illuminate\Http\Resources\Json\JsonResource;
class ServiceResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->resource->id,
'name' => $this->resource->name,
'url' => $this->resource->url,
];
}
}
To use this resource in your controller and return a collection of services, do the following:
public function index(Request $request): Response
{
return ServiceResource::collection(
resource: Service::query()->simplePaginate(),
)->toResponse(
request: $request,
);
}
With ServiceResource
, you ensure that the API response includes only the necessary data for clients, keeping it clean and efficient.
Enhancing Your Resources with Additional Data
Sometimes, you’ll want to include additional data, such as formatted dates, in your API response. Let’s create another resource to format dates more readably:
php artisan make:resource V1/DateResource
Here’s an example of how you can format dates in this resource:
namespace App\Http\Resources\V1;
use Illuminate\Http\Resources\Json\JsonResource;
use Carbon\Carbon;
class DateResource extends JsonResource
{
public function toArray($request): array
{
return [
'human' => $this->resource->diffForHumans(),
'string' => $this->resource->toDateTimeString(),
'local' => $this->resource->toDateTimeLocalString(),
];
}
}
By leveraging Carbon, a popular Laravel package for date manipulation, you can provide users with easily readable date formats.
Implementing JSON API Specification
While Laravel's default resource structure is flexible, following a standardized format like JSON API can make your API easier to use for clients.
JSON API defines a clear structure for your responses, including fields like id
, type
, attributes
, and relationships
.
Using the JSON API Resource Package
To implement the JSON API standard, install the following package:
composer require tim-mcdonald/laravel-json-api
After installing, update your ServiceResource
to adhere to the JSON API format:
namespace App\Http\Resources\V1;
use TimMcDonald\JsonApi\JsonApiResource;
class ServiceResource extends JsonApiResource
{
public function attributes(): array
{
return [
'name' => $this->name,
'url' => $this->url,
];
}
}
This format will ensure that your responses are structured according to the JSON API specification, which is widely recognized across the industry.
Adding Relationships in Laravel 11
In more complex APIs, you’ll often need to include related resources, such as checks related to a service. Here’s how you can include these relationships in your API responses:
public function with($request)
{
return [
'links' => [
'self' => route('services.show', ['service' => $this->id]),
'checks' => route('checks.index'),
],
];
}
In your CheckResource
, you can define how these related entities are represented:
namespace App\Http\Resources\V1;
use TimMcDonald\JsonApi\JsonApiResource;
class CheckResource extends JsonApiResource
{
public function attributes()
{
return [
'name' => $this->name,
'path' => $this->path,
];
}
}
This setup allows you to link related resources and provide more meaningful data in your API responses.
Leveraging Laravel Query Builder for Advanced Filtering
To build more powerful APIs, consider using the Laravel Query Builder package by Spatie. This package simplifies complex queries, allowing you to filter and include relationships dynamically. To install the package, run:
composer require spatie/laravel-query-builder
After installation, you can use the package in your controller like this:
use Spatie\QueryBuilder\QueryBuilder;
public function index(Request $request): Response
{
$services = QueryBuilder::for(Service::class)
->allowedIncludes(['checks'])
->allowedFilters(['url'])
->getEloquentBuilder()
->simplePaginate();
return ServiceResource::collection($services)->toResponse($request);
}
This package provides a clean way to handle queries, making your API more versatile and easier to use.
Additional Resources to Explore
As you continue to build APIs in Laravel 11, here are some additional tools and resources that can enhance your development process: