Laravel Tutorial: Creating a Headless System with Vercel PostgreSQL and CRUD Operations

Laravel Tutorial: Creating a Headless System with Vercel PostgreSQL and CRUD Operations

This tutorial will guide you through setting up a headless Laravel application with Vercel PostgreSQL as the database. We will cover how to create CRUD (Create, Read, Update, Delete) operations for a simple system.

40 Open-source Free Laravel Projects, CMS, ERP, Headless, CRM and More
Laravel is an innovative and powerful web application framework that is completely free and open-source. This PHP-based framework was created by Taylor Otwell in 2011 as an alternative to CodeIgniter, with a focus on simplicity and ease of use. At its core, Laravel is built on the Model-View-Controller (MVC) architecture

Prerequisites

  • Basic knowledge of Laravel and PHP.
  • A Vercel account.
  • Node.js and Composer installed on your machine.

Set Up Laravel Project

Configure Environment:

Copy the .env.example file to .env and update the configuration:

cp .env.example .env

Generate an application key:

php artisan key:generate

Install Laravel:

Open your terminal and run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel headless-laravel

Navigate into the project directory:

cd headless-laravel

Set Up Vercel PostgreSQL

Create a Vercel Project:
Log in to your Vercel account and create a new project. Connect your GitHub repository where your Laravel project is hosted.

Add PostgreSQL to Vercel:
Go to the Vercel dashboard, navigate to the "Integrations" tab, and add PostgreSQL. Vercel will provide the database URL.

Update Laravel Environment:
In your .env file, update the database configuration:

DB_CONNECTION=pgsql
DB_HOST=your-database-host
DB_PORT=5432
DB_DATABASE=your-database-name
DB_USERNAME=your-database-username
DB_PASSWORD=your-database-password

Create Models and Migrations

Define the Migration:

Open the migration file in database/migrations/ and define the schema:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('content');
        $table->timestamps();
    });
}

Run the migration to create the posts table:

php artisan migrate

Generate a Model:

Let's create a Post model with a migration file:

php artisan make:model Post -m

The -m flag creates a migration file for the Post model.

Create CRUD Operations

Implement Controller Methods:
Open PostController.php and implement the CRUD operations:

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        return Post::all();
    }

    public function show($id)
    {
        return Post::find($id);
    }

    public function store(Request $request)
    {
        $post = Post::create($request->all());
        return response()->json($post, 201);
    }

    public function update(Request $request, $id)
    {
        $post = Post::findOrFail($id);
        $post->update($request->all());
        return response()->json($post, 200);
    }

    public function destroy($id)
    {
        Post::destroy($id);
        return response()->json(null, 204);
    }
}

Define Routes:


Open routes/api.php and add routes for the CRUD operations:

use App\Http\Controllers\PostController;

Route::get('posts', [PostController::class, 'index']);
Route::get('posts/{id}', [PostController::class, 'show']);
Route::post('posts', [PostController::class, 'store']);
Route::put('posts/{id}', [PostController::class, 'update']);
Route::delete('posts/{id}', [PostController::class, 'destroy']);

Create Controller:

Generate a controller for handling CRUD operations:

php artisan make:controller PostController

Deploy to Vercel

Commit Changes: Ensure all changes are committed to your Git repository.

Deploy via Vercel:
Push your repository to GitHub, and Vercel will automatically deploy the changes. Vercel will detect the Laravel framework and set up the environment accordingly.

Access the API:
Once the deployment is complete, you can access your API endpoints through the Vercel URL, for example, https://your-project.vercel.app/api/posts.

Test the API

You can test the API using tools like Postman or cURL to ensure that all CRUD operations work as expected.

12 Open-source API Testing Frameworks for REST-API and GraphQL
API testing frameworks are tools or libraries that provide a structured approach to testing Application Programming Interfaces (APIs). They offer a set of functions, methods, and utilities to automate the process of API testing. Purpose of API Testing Frameworks API testing frameworks are used to: * Automate the testing of APIs
  • List all posts: GET /api/posts
  • Get a specific post: GET /api/posts/{id}
  • Create a new post: POST /api/posts
  • Update a post: PUT /api/posts/{id}
  • Delete a post: DELETE /api/posts/{id}

Conclusion

You've successfully set up a headless Laravel system with Vercel PostgreSQL and implemented CRUD operations. This setup is now ready for further development or integration with a front-end application.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+