Laravel 12 + PostgreSQL CRUD Tutorial for Beginners (Step-by-Step Guide)
Introduction: “I Just Want to Build Something… But Where Do I Start?”
Imagine this: You're staring at your laptop, filled with excitement to build your first web application. You’ve heard great things about Laravel — how it's elegant, modern, and developer-friendly. You’ve also heard PostgreSQL is a rock-solid database system used by giants like Instagram and Reddit. But then, you pause...
"How do I even connect Laravel to PostgreSQL? And what’s CRUD anyway?"
Don’t worry — you’re not alone! If you’re a beginner, this tutorial is designed just for you. We'll walk through every step of setting up Laravel 12 with PostgreSQL and building a full CRUD (Create, Read, Update, Delete) system.
By the end, you'll have a simple app up and running, and most importantly — you’ll understand how it works.
Let’s get building! 🔨
🧰 What You’ll Need (Requirements)
Before diving in, make sure you have the following installed:
- PHP ≥ 8.2
- Composer
- Laravel 12
- PostgreSQL
- A code editor (like VS Code)
- Postman (optional, for testing APIs)
📦 Step 1: Create a New Laravel Project
composer create-project laravel/laravel laravel12-postgres-crud
Navigate into your project:
cd laravel12-postgres-crud
🛠 Step 2: Configure PostgreSQL in Laravel
Open the .env file in your project and update the database section:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_crud
DB_USERNAME=your_postgres_user
DB_PASSWORD=your_postgres_password
Create the database in PostgreSQL:
CREATE DATABASE laravel_crud;
👉 Tip: Use pgAdmin if you're more comfortable with a GUI!
🧱 Step 3: Create a Model, Migration, and Controller
Let’s say we’re building a Book Management App. Run this command:
php artisan make:model Book -mcr
This will generate:
Book.php(Model)- A migration file
BookController.php
📄 Step 4: Define the Database Schema
Open the migration file in database/migrations/xxxx_xx_xx_create_books_table.php and edit it like this:
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('author');
$table->text('description')->nullable();
$table->timestamps();
});
}
Run the migration:
php artisan migrate
👏 Now your books table is ready in PostgreSQL!
✍️ Step 5: Set Up Routes (web.php)
In routes/web.php, add:
use App\Http\Controllers\BookController;
Route::resource('books', BookController::class);
This creates RESTful routes like:
/books(GET) → list books/books/create(GET) → form to add book/books(POST) → save new book/books/{id}(GET) → show one book/books/{id}/edit(GET) → edit book form/books/{id}(PUT/PATCH) → update book/books/{id}(DELETE) → delete book
🎨 Step 6: Create Views Using Blade
Create a new folder: resources/views/books
Here are some basic views:
📘 index.blade.php
<h1>All Books</h1>
<a href="{{ route('books.create') }}">➕ Add Book</a>
@foreach($books as $book)
<div>
<h3>{{ $book->title }} by {{ $book->author }}</h3>
<p>{{ $book->description }}</p>
<a href="{{ route('books.edit', $book) }}">✏️ Edit</a>
<form method="POST" action="{{ route('books.destroy', $book) }}">
@csrf
@method('DELETE')
<button type="submit">🗑 Delete</button>
</form>
</div>
@endforeach
(Repeat similarly for create.blade.php and edit.blade.php)
🧠 Step 7: Fill In the Controller Logic
Here’s a basic BookController.php:
use App\Models\Book;
use Illuminate\Http\Request;
public function index() {
$books = Book::all();
return view('books.index', compact('books'));
}
public function create() {
return view('books.create');
}
public function store(Request $request) {
Book::create($request->all());
return redirect()->route('books.index');
}
public function edit(Book $book) {
return view('books.edit', compact('book'));
}
public function update(Request $request, Book $book) {
$book->update($request->all());
return redirect()->route('books.index');
}
public function destroy(Book $book) {
$book->delete();
return redirect()->route('books.index');
}
Don't forget to allow mass assignment in Book.php:
protected $fillable = ['title', 'author', 'description'];
✅ Step 8: Test It!
Run the Laravel development server:
php artisan serve
Visit http://127.0.0.1:8000/books — your CRUD app is now live!
🎉 You did it! From project setup to full PostgreSQL integration, you're now managing data in a real web app.
🧠 Recap: What Did You Learn?
- How to install Laravel 12
- Connect Laravel to a PostgreSQL database
- Create a model, migration, controller, and views
- Perform full CRUD operations
- Structure a beginner-friendly web app
📣 Your Turn!
Now it’s time to customize this app — maybe add more fields, user authentication, or even use Bootstrap for styling. Every step you take builds confidence and skills!
💬 What will you build next with Laravel? Share your ideas in the comments below!
❓FAQ: Common Beginner Questions
Q1: Why choose PostgreSQL over MySQL?
A: PostgreSQL offers advanced features (like JSONB, full-text search, etc.) and better compliance with SQL standards.
Q2: What is a migration in Laravel?
A: A migration is a version control system for your database. It allows you to define and modify your DB schema using PHP.
Q3: Can I use this CRUD app as a REST API?
A: Absolutely! Just modify your controller methods to return JSON and remove the Blade views.
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.