{
"id": "php",
"data": {
"slug": "php",
"label": "PHP",
"icon": "tech/laravel",
"colors": {
"background": "#787cb5",
"color": "#fff"
}
},
"filePath": "src/content/tags.json",
"collection": "tags"
} [
{
"id": "guides/laravel_rest_api",
"data": {
"title": "How to Build a Small REST API with Laravel 11",
"description": "In this post we're gonna learn how to build a simple and powerful REST API using Laravel 11. A step-by-step guide for developers using the latest Laravel features.",
"pubDate": "2025-04-24T00:00:00.000Z",
"tags": [
{
"id": "laravel",
"collection": "tags"
},
{
"id": "php",
"collection": "tags"
},
{
"id": "rest_api",
"collection": "tags"
}
],
"category": {
"id": "guides",
"collection": "categories"
},
"author": "Leandro Gomez"
},
"body": "## Let's get started!\n\nLaravel has become one of the most preferred PHP frameworks for web application development, and its smooth syntax combined with robust features makes it an ideal choice for creating RESTful APIs. In this tutorial, we'll walk through the steps to create a simple REST API using Laravel 11, tailored specifically for tech enthusiasts and developers looking to enhance their skills.\n\n#### Prerequisites\n\nBefore we begin, ensure you have the following installed on your machine:\n\n1. PHP (at least version 8.1)\n2. Composer\n3. Laravel 11\n4. A database (MySQL, SQLite, etc.)\n\n#### Step 1: Setting Up Laravel \n\nStart by creating a new Laravel project. Open your terminal and run the following command:\n\n```bash\ncomposer create-project --prefer-dist laravel/laravel laravel-rest-api\n```\n\nChange your directory into the newly created project:\n\n```bash\ncd laravel-rest-api\n```\n\nNext, set up your environment variables. Copy `.env.example` to `.env`:\n\n```bash\ncp .env.example .env\n```\n\nYou need to configure your database settings in the `.env` file. Update the following lines to match your database configuration:\n\n```env\nDB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=[your_database_name]\nDB_USERNAME=[your_username]\nDB_PASSWORD=[your_password]\n```\n\nAfter updating the `.env` file, generate your application key:\n\n```bash\nphp artisan key:generate\n```\n\n#### Step 2: Building the API Resource\n\nFor this tutorial, we will create an API for managing products. Start by creating a Product model along with a migration:\n\n```bash\nphp artisan make:model Product -m\n```\n\nOpen the generated migration file in `database/migrations/`, and define your product schema:\n\n```php\npublic function up()\n{\n Schema::create('products', function (Blueprint $table) {\n $table->id();\n $table->string('name');\n $table->decimal('price', 8, 2);\n $table->text('description');\n $table->timestamps();\n });\n}\n```\n\nRun the migration to create the `products` table:\n\n```bash\nphp artisan migrate\n```\n\n#### Step 3: Creating the Controller\n\nNow, let's create a controller that will handle our REST API logic:\n\n```bash\nphp artisan make:controller ProductController --api\n```\n\nIn the `ProductController`, implement the following methods:\n\n```php\npublic function index()\n{\n return Product::all();\n}\n\npublic function store(Request $request)\n{\n $validated = $request->validate([\n 'name' => 'required|max:255',\n 'price' => 'required|numeric',\n 'description' => 'nullable',\n ]);\n return Product::create($validated);\n}\n\npublic function show($id)\n{\n return Product::findOrFail($id);\n}\n\npublic function update(Request $request, $id)\n{\n $product = Product::findOrFail($id);\n $validated = $request->validate([\n 'name' => 'required|max:255',\n 'price' => 'required|numeric',\n 'description' => 'nullable',\n ]);\n $product->update($validated);\n return $product;\n}\n\npublic function destroy($id)\n{\n $product = Product::findOrFail($id);\n $product->delete();\n return response()->noContent();\n}\n```\n\n#### Step 4: Defining the Routes\n\nIn `routes/api.php`, add the following routes to connect our controller methods:\n\n```php\nuse App\\Http\\Controllers\\ProductController;\n\nRoute::apiResource('products', ProductController::class);\n```\n\n#### Step 5: Testing the API\n\nYou can test your API using tools like Postman or Insomnia. Use the following endpoints to interact with your API:\n\n- **GET** `/api/products` - List all products\n- **POST** `/api/products` - Create a new product\n- **GET** `/api/products/{id}` - Retrieve a specific product\n- **PUT** `/api/products/{id}` - Update a product\n- **DELETE** `/api/products/{id}` - Delete a product\n\n#### Conclusion\n\nCongratulations! You've successfully set up a REST API using Laravel 11. This tutorial provides a foundational blueprint to build upon. As you continue enhancing the API, consider implementing features such as authentication, rate limiting, and pagination for a more robust solution. Enjoy coding!",
"filePath": "src/content/blog/guides/laravel_rest_api.mdx",
"digest": "ca5722a238701825",
"deferredRender": true,
"collection": "blog"
}
]