Installation
This guide provides detailed instructions for setting up Laravel Breeze API + Next.js in your development environment.Prerequisites
Before you begin, ensure your system meets these requirements:Required Software
- PHP 8.2 or higher - Verify with
php --version - Composer - PHP dependency manager (Download)
- Node.js 18+ - JavaScript runtime (Download)
- npm or pnpm - Package manager (pnpm recommended)
- Git - Version control
Database Options
- SQLite - Default for development (no installation required)
- MySQL 5.7+ or MariaDB 10.3+ (Optional)
- PostgreSQL 10+ (Optional)
Verify Installation
Check that all required tools are installed:Backend Setup (Laravel)
1. Clone the Repository
First, clone the repository and navigate to the project directory:2. Install Backend Dependencies
Navigate to the Backend directory and install PHP dependencies:- Laravel Framework 12.x
- Laravel Sanctum 4.x
- Laravel Breeze 2.x
- Pest PHP testing framework
- Other required packages
3. Environment Configuration
Copy the example environment file and generate an application key:4. Configure Environment Variables
Open the.env file and configure the following settings:
Basic Application Settings
Frontend URL Configuration
This is crucial for CORS to work properly:
Database Configuration
- SQLite (Recommended for Development)
- MySQL
- PostgreSQL
Session and Cache Configuration
Mail Configuration (for Password Reset)
- Log (Development)
- SMTP (Production)
storage/logs/laravel.log5. Run Database Migrations
Create the database tables:- Users
- Password resets
- Sessions
- Cache
- Jobs queue
- Personal access tokens (Sanctum)
6. (Optional) Seed the Database
Populate the database with sample data:7. Configure CORS
The CORS configuration is located atconfig/cors.php. By default, it’s configured to accept requests from your frontend:
8. Verify Backend Installation
Start the Laravel development server:Frontend Setup (Next.js)
1. Navigate to Frontend Directory
Open a new terminal and navigate to the Frontend directory:2. Install Frontend Dependencies
- pnpm (Recommended)
- npm
- yarn
- Next.js 16.x
- React 19.x
- TypeScript 5.x
- Tailwind CSS 4.x
- Headless UI
- Formik & Yup
- Axios & SWR
- Other dependencies
3. Configure Environment Variables
Create a.env.local file in the Frontend directory:
.env.local and set the backend URL:
The
NEXT_PUBLIC_ prefix makes this variable accessible in the browser.4. Verify Frontend Configuration
The frontend is configured to work with the backend through theNEXT_PUBLIC_BACKEND_URL environment variable. All API calls will be made to this URL.
Key configuration files:
src/lib/axios.ts- Axios configuration for API callssrc/hooks/auth.ts- Authentication hookstailwind.config.ts- Tailwind CSS configurationnext.config.js- Next.js configuration
5. Run Frontend Development Server
- pnpm
- npm
- yarn
Running Both Applications
Option 1: Automated (Recommended)
From the Backend directory, run:composer.json that runs:
- Laravel server on port 8000
- Queue worker for background jobs
- Next.js development server on port 3000
concurrently.
Option 2: Manual
Run each service in a separate terminal: Terminal 1 - Laravel Server:Testing the Installation
1. Test Backend API
With the backend running, test the user endpoint:{"message":"Unauthenticated."}
2. Test Frontend
- Visit http://localhost:3000
- You should see the login page
- Click “Register” to create a new account
- Fill out the registration form:
- Name
- Password
- Confirm Password
- Submit the form
3. Test Authentication Flow
After registration:- You should be automatically logged in
- The dashboard page should load
- Your user information should be displayed
- Try logging out and logging back in
4. Test Password Reset (Optional)
- Click “Forgot Password” on the login page
- Enter your email address
- Check
storage/logs/laravel.logfor the reset link - Follow the link to reset your password
Running Tests
Backend Tests
Run the Pest test suite:Frontend Linting
Production Build
Backend Production Setup
.env for production:
Frontend Production Build
.env.local for production:
Troubleshooting
CORS Issues
Symptom: “CORS policy” errors in browser console Solution:- Verify
FRONTEND_URLin backend.envmatches your frontend URL - Ensure
config/cors.phphas'supports_credentials' => true - Clear config cache:
php artisan config:clear - Restart the backend server
419 CSRF Token Mismatch
Symptom: 419 error when submitting forms Solution:- Verify
SANCTUM_STATEFUL_DOMAINSincludes your frontend domain - Ensure cookies are being sent with requests
- Check that
withCredentialsis set in Axios configuration
Database Connection Errors
Symptom: “Could not find driver” or connection refused errors Solution:- For SQLite: Ensure
php-sqlite3extension is installed - For MySQL: Install
php-mysqlextension - For PostgreSQL: Install
php-pgsqlextension - Verify database credentials in
.env - Ensure database exists
Port Already in Use
Symptom: “Address already in use” error Solution:Module Not Found Errors
Symptom: Frontend shows “Module not found” errors Solution:Next Steps
Now that your installation is complete:- Explore the codebase structure
- Review the authentication implementation
- Start building your application features
- Check out the Laravel Documentation
- Read the Next.js Documentation
- Learn about Laravel Sanctum