Getting Started with Laravel
Laravel is one of the most popular PHP frameworks in the world — and for good reason. It provides a clean, expressive syntax that lets you focus on building your application rather than fighting the framework. Whether you're coming from raw PHP, another framework, or a completely different language, Laravel's learning curve is gentle enough to get productive quickly while deep enough to sustain you for years.
Prerequisites
Before diving in, make sure you have the following installed on your machine:
- PHP 8.2 or higher — Laravel 12 requires modern PHP features like enums, fibers, and readonly properties.
- Composer — PHP's dependency manager, used to install Laravel and its packages.
- A database — MySQL, PostgreSQL, or SQLite all work. SQLite is the easiest for local development.
- Node.js & npm — needed if you plan to compile frontend assets with Vite.
You can verify your PHP version with:
php -v
Installing Laravel
The recommended way to create a new Laravel project is via Composer:
composer create-project laravel/laravel my-app
cd my-app
php artisan serve
Your application is now running at http://localhost:8000. That's it — no configuration needed to get started.
Alternatively, if you have the Laravel installer:
composer global require laravel/installer
laravel new my-app
The installer offers an interactive prompt that lets you choose your starter kit, database, testing framework, and more.
Understanding the Directory Structure
Laravel's folder layout is thoughtfully organized. The key directories you'll work with most:
| Directory | Purpose |
|---|---|
app/ |
Your application's core logic — models, controllers, services |
routes/ |
All route definitions (web.php, api.php, console.php) |
resources/views/ |
Blade templates |
database/ |
Migrations, factories, seeders |
config/ |
Configuration files for every aspect of the framework |
storage/ |
Logs, compiled views, file uploads |
Your First Route
Open routes/web.php. You'll see a single example route. Let's add one:
Route::get('/hello', function () {
return 'Hello, Laravel!';
});
Visit http://localhost:8000/hello and you'll see your response. Routes can return strings, arrays (automatically JSON-encoded), or Blade views.
For anything beyond trivial logic, routes should hand off to a controller:
Route::get('/posts', [PostController::class, 'index'])->name('posts.index');
Named routes let you generate URLs anywhere in your app with route('posts.index') — meaning you never hardcode URLs again.
Controllers
Generate a controller with Artisan, Laravel's command-line tool:
php artisan make:controller PostController
A controller is a plain PHP class. Each public method handles a request:
class PostController extends Controller
{
public function index()
{
$posts = Post::latest()->paginate(10);
return view('posts.index', compact('posts'));
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
}
Notice show(Post $post) — Laravel automatically resolves the Post model from the route parameter via route model binding. No manual Post::findOrFail($id) needed.
Blade Templating
Blade is Laravel's templating engine. It compiles down to plain PHP but adds a clean, readable layer on top:
@extends('layouts.app')
@section('content')
<h1>Blog Posts</h1>
@foreach ($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
<p>{{ $post->excerpt }}</p>
<a href="{{ route('posts.show', $post) }}">Read more</a>
</article>
@endforeach
{{ $posts->links() }}
@endsection
{{ }} escapes output to prevent XSS. Use {!! !!} only when you explicitly trust the content (like rendered HTML). The {{ $posts->links() }} call renders pagination controls automatically.
The Eloquent ORM
Eloquent is Laravel's ORM, and it's one of the framework's most beloved features. Create a model:
php artisan make:model Post -m
The -m flag generates a migration alongside the model. Open the migration:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt')->nullable();
$table->longText('body');
$table->boolean('published')->default(false);
$table->timestamps();
});
Run the migration:
php artisan migrate
Now your Post model works out of the box:
// Create
Post::create(['title' => 'Hello World', 'slug' => 'hello-world', 'body' => '...']);
// Query
$posts = Post::where('published', true)->latest()->get();
// Update
$post->update(['title' => 'Updated Title']);
// Delete
$post->delete();
Environment Configuration
Laravel uses a .env file for environment-specific settings — database credentials, mail config, API keys. Never commit this file to version control.
APP_NAME=MyApp
APP_ENV=local
APP_DEBUG=true
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=my_app
DB_USERNAME=root
DB_PASSWORD=secret
Access values in your code through the config() helper, never env() directly — config files cache cleanly, env() calls do not.
Artisan: Your Best Friend
Artisan is Laravel's CLI tool. You'll use it constantly:
php artisan make:model Post -mcrf # model + migration + controller + resource + factory
php artisan migrate # run pending migrations
php artisan migrate:fresh --seed # drop all tables, re-migrate, seed
php artisan route:list # list all registered routes
php artisan tinker # REPL with full app context
php artisan config:cache # cache config for production
The
php artisan tinkercommand gives you a full PHP REPL with your entire Laravel application loaded. It's indispensable for debugging — you can query the database, test methods, and inspect data without writing a throwaway script.
What's Next?
This covers the essential foundation. From here, explore:
- Authentication — Laravel Breeze or Jetstream give you a complete auth scaffold in minutes.
- Queues & Jobs — offload slow tasks like email sending and image processing to background workers.
- Events & Listeners — decouple your application logic with an event-driven architecture.
- Testing — Laravel ships with Pest and PHPUnit support, plus helpers that make HTTP and database testing a pleasure.
Laravel's official documentation at laravel.com is one of the best in the industry. Every feature is covered thoroughly with examples. Bookmark it — you'll visit it daily.
Categories
- All categories 6
- Development 3
- API 1
- PHP 1
- Database 1