Laravel 12 has made it even easier for developers to build fast, elegant, and maintainable web applications. Whether you're completely new to web development or transitioning from another framework, this comprehensive guide will walk you through setting up your first Laravel project and understanding its core concepts.
What is Laravel 12?
Laravel is a popular PHP framework known for its elegant syntax and developer-friendly features. Laravel 12, the latest version, brings enhanced performance, improved developer tools, and streamlined workflows that make building modern web applications faster and more enjoyable.
Prerequisites
Before you begin, make sure you have the following installed on your system:
- PHP 8.2 or higher - Laravel 12 requires modern PHP features
- Composer - PHP's dependency manager for installing Laravel
- Database (MySQL, PostgreSQL, or SQLite) - For data persistence
- Node.js & NPM (optional) - For frontend asset compilation
1. Installing Laravel 12 via Composer
The quickest and most reliable way to create a new Laravel project is using Composer, PHP's package manager.
Installation Steps
Open your terminal or command prompt and run the following commands:
composer create-project laravel/laravel laravel-app
cd laravel-app
php artisan serve
This will:
- Download and install Laravel 12 with all its dependencies
- Create a new project directory called
laravel-app - Start the built-in PHP development server
Accessing Your Application
Once the server is running, open your web browser and navigate to http://localhost:8000. You should see Laravel's beautifully designed default welcome page, confirming that your installation was successful.
2. Understanding Laravel's Folder Structure
Laravel's directory structure is thoughtfully organized to keep your code clean and maintainable. Here's what each key folder contains:
Core Directories
app/ - The heart of your application
- Contains all your application logic including models, controllers, middleware, and service providers
- This is where you'll spend most of your development time
routes/ - Application routing
web.php- Defines routes for your web interfaceapi.php- Contains API routes with token authenticationconsole.php- Registers custom Artisan commands
resources/ - Frontend assets and views
views/- Blade template files for your UIcss/andjs/- Frontend assets that can be compiled with Vite
database/ - Database management
migrations/- Version control for your database schemaseeders/- Populate your database with test datafactories/- Generate fake data for testing
config/ - Configuration files
- All application settings organized by service (database, mail, cache, etc.)
public/ - Web server document root
- The only directory accessible to web browsers
- Contains
index.php(entry point) and compiled assets
storage/ - Generated files
- Logs, compiled views, file uploads, and cache files
- Needs write permissions for the web server
tests/ - Automated testing
- Write PHPUnit and Feature tests to ensure code quality
vendor/ - Composer dependencies
- Never edit files here directly
- Managed automatically by Composer
3. Creating Your First Route
Routes are the entry points to your application. They map URLs to specific actions or responses.
Basic Route Example
Open the routes/web.php file and add this simple route:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Welcome to My First Laravel App!';
});
This route responds to GET requests at the root URL (/) and returns a simple text message.
Understanding Route Syntax
Route::get()- Handles HTTP GET requests- First parameter - The URI path (
'/') - Second parameter - A closure or controller method that defines what to return
Testing Your Route
Save the file and refresh your browser. Instead of the default Laravel welcome page, you should now see your custom message: "Welcome to My First Laravel App!"
4. Building Your First Controller
While closures work for simple routes, controllers provide better organization for complex application logic.
Generating a Controller
Laravel's Artisan CLI tool makes creating controllers effortless:
php artisan make:controller HomeController
This command creates a new file at app/Http/Controllers/HomeController.php.
Adding Controller Logic
Open the newly created HomeController.php and add an index method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Display the homepage
*
* @return \Illuminate\View\View
*/
public function index()
{
return view('welcome');
}
}
Connecting Routes to Controllers
Now update your route in routes/web.php to use the controller:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
This approach separates your routing logic from business logic, making your application more maintainable as it grows.
Benefits of Using Controllers
- Organization - Keep related functionality grouped together
- Reusability - Methods can be used by multiple routes
- Testability - Easier to write unit tests for controller methods
- Middleware - Apply middleware to specific controller methods
- Resource Controllers - Automatic CRUD operation routing
5. Why Laravel 12 Stands Out
Laravel has consistently been one of the most popular PHP frameworks, and version 12 continues this tradition with several compelling advantages:
Simple, Elegant Syntax
Laravel's expressive syntax makes code readable and enjoyable to write. Tasks that require dozens of lines in other frameworks can often be accomplished in just a few lines with Laravel.
Improved Performance
Laravel 12 includes significant performance optimizations:
- Faster routing engine
- Improved query builder efficiency
- Better caching mechanisms
- Optimized autoloading
Robust Developer Tools
Out-of-the-box, Laravel provides:
- Artisan CLI - Powerful command-line interface for scaffolding and automation
- Tinker - Interactive REPL for experimenting with your application
- Telescope - Debugging assistant for monitoring requests, exceptions, and queries
- Sail - Docker-based development environment
Active Community and Ecosystem
Laravel boasts one of the most vibrant PHP communities:
- Extensive documentation and tutorials
- Thousands of packages available via Composer
- Regular updates and security patches
- Laracasts video tutorials
- Active forums and Discord channels
Built-in Features
Laravel includes everything you need for modern web development:
- Authentication and authorization systems
- Database migrations and ORM (Eloquent)
- Queue management for background jobs
- Email sending capabilities
- File storage abstraction
- Task scheduling
- Real-time event broadcasting
Next Steps in Your Laravel Journey
Congratulations! You've successfully set up your first Laravel application, created routes, and built a controller. Here are some recommended next steps to continue your learning:
Master Blade Templates
Learn Laravel's powerful templating engine to create dynamic, reusable views with:
- Template inheritance and layouts
- Components and slots
- Conditional rendering
- Loops and iterations
Connect a Database
Set up database connectivity and learn about:
- Eloquent ORM for elegant database interactions
- Migration system for version-controlled schemas
- Query builder for complex database queries
- Model relationships (one-to-many, many-to-many)
Build RESTful APIs
Laravel excels at API development:
- Resource controllers for standard CRUD operations
- API authentication with Laravel Sanctum
- JSON response formatting
- Rate limiting and throttling
Implement Authentication
Add user registration and login functionality:
- Laravel Breeze for simple authentication scaffolding
- Laravel Jetstream for more advanced features
- Password reset functionality
- Email verification
Explore Advanced Features
Once comfortable with the basics, dive into:
- Middleware for request filtering
- Service providers for bootstrapping
- Events and listeners for decoupled architecture
- Job queues for asynchronous processing
- Testing with PHPUnit and Pest
Conclusion
You've just built your first Laravel application and learned the fundamental concepts that power this elegant framework. Laravel's flexibility and comprehensive feature set allow you to grow from beginner to professional developer without needing to switch frameworks.
The journey from this simple "Hello World" application to a full-featured web application is straightforward with Laravel's excellent documentation and supportive community. Start small, experiment often, and gradually incorporate more features as your confidence grows.
Remember, every expert Laravel developer started exactly where you are now. The key is consistent practice and building real projects. So pick an idea, start coding, and enjoy the development experience that Laravel provides.