Building JSON APIs with Laravel
Learn how to create RESTful APIs using api.php routes and API resources.
Laravel is widely used to build backend APIs for modern frontends. ## API routes ```php Route::get('/posts', [ApiPostController::class, 'index']); ``` ## Return JSON ```php public function index() { return Post::latest()->paginate(10); } ``` ## API Resource ```bash php artisan make:resource PostResource ``` ```php public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'created_at' => $this->created_at->toISOString(), ]; } ``` ## API flow ```mermaid flowchart LR A[Client] --> B[API Route] B --> C[Controller] C --> D[Resource] D --> E[JSON] ``` APIs allow your Laravel app to serve web and mobile clients. In the next tutorial, we will secure APIs using Laravel Sanctum.