Laravel

Building a REST API with Laravel from Scratch

From routes to tidy JSON responses: we build a simple product API complete with validation.

C Codesakura 2分 コメント3件

目次

A REST API lets other apps (a mobile app, a React frontend, or a third-party service) talk to yours. In this part we build a simple product API: list, create, and delete.

Setting up the routes

Laravel ships with Route::apiResource, which registers every CRUD route at once. Add this to routes/api.php:

Route::apiResource('products', ProductController::class);

Writing the controller

The controller simply returns models. Laravel turns them into JSON for you.

public function index()
{
  return Product::latest()->paginate(20);
}

Next steps

The API is still open to anyone. In the next part we add token authentication so only registered users can create products.

シリーズの一部

Laravel from Scratch
  1. 1Setting Up Your First Laravel Project
  2. 2Getting to Know Routing and Controllers in Laravel
  3. 3Building a REST API with Laravel from Scratch
共有 X Facebook WhatsApp Telegram

コメント3件

コメントするにはログインしてください。 ログイン

D

Dewi S.

Ditunggu lanjutannya.

R

Rizky A.

Penjelasannya runtut, langsung saya coba dan jalan. Terima kasih!

C

Codesakura 著者

Senang bisa membantu. Kalau ada yang macet, tulis saja di sini.

関連チュートリアル

Laravel

Getting to Know Routing and Controllers in Laravel

2分 シリーズ · 第2回(全3回)

Laravel

Setting Up Your First Laravel Project

1分 シリーズ · 第1回(全3回)

Laravel

Secure File Uploads in Laravel

2分