Laravel is one of the most popular PHP frameworks, known for its elegance and developer-friendly features.
If you’re just starting your Laravel journey, this guide will give you a brief introduction to some of the most important concepts youโll encounter. Letโs dive in!
1. Routing and Controllers ๐ฃ๏ธ
In Laravel, routes define how your application responds to incoming requests. They match URLs to the right controller methods.
- Routes are defined in:
routes/web.php
(for web routes) androutes/api.php
(for API routes). - Controllers are classes where you group related logic, like handling user requests for products, orders, or posts.
Example route:
Route::get('/products', [ProductController::class, 'index'])
This maps /products
to the index
method of ProductController
.
2. Blade Templating Basics ๐จ
Laravel uses Blade as its templating engine to create dynamic views.
- Blade files end with
.blade.php
. - Blade allows you to use PHP code in views with a clean syntax.
- Common features: loops, conditions, layout inheritance, and components.
Example:
@if($products->count())
@foreach($products as $product)
<p>{{ $product->name }}</p>
@endforeach
@else
<p>No products found.</p>
@endi
3. Authentication Starter Kits ๐
Laravel provides easy-to-use starter kits to handle user registration, login, password reset, and more.
- Popular starter kits:
- Laravel Breeze: Simple and minimal.
- Laravel Jetstream: Advanced features like two-factor auth and team management.
You can install one with:
composer require laravel/breeze --dev
php artisan breeze:instal
4. Middleware and Route Model Binding ๐
Middleware
Middleware are filters that run before or after a request. They are used for things like:
- Checking if a user is authenticated.
- Adding CORS headers.
- Logging requests.
Example: Only allow logged-in users to access a route.
Route::get('/dashboard', function () {
return 'Welcome!';
})->middleware('auth')
Route Model Binding
This is a shortcut for automatically fetching a model from the database when a route parameter is passed.
Example:
Route::get('/products/{product}', function (Product $product) {
return $product;
})
The {product}
automatically fetches the product by ID.
5. Eloquent ORM: Relationships, Mutators, Accessors ๐๏ธ
Eloquent ORM
Eloquent is Laravelโs powerful database layer that makes interacting with databases easy and expressive.
Relationships
Eloquent makes defining relationships simple:
- One to One
- One to Many
- Many to Many
Example:
public function products() {
return $this->hasMany(Product::class);
}
Mutators & Accessors
- Accessors: Format data when getting.
- Mutators: Format data when setting.
Example Accessor:
public function getPriceWithCurrencyAttribute() {
return '$' . $this->price;
}
6. Database Migrations and Seeders ๐ฆ
Migrations
Migrations are version control for your database. They define how tables and columns are created.
Example:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 8, 2);
});
Run migrations with:
php artisan migrate
Seeders
Seeders insert sample data into tables.
DB::table('products')->insert([
'name' => 'Sample Product',
'price' => 99.99
]);
7. Request Validation and Form Requests โ
Laravel provides easy validation tools to check incoming form data.
Example Direct Validation
$request->validate([
'name' => 'required|string',
'price' => 'required|numeric',
]);
Form Requests
A Form Request is a dedicated class to handle validation logic.
php artisan make:request ProductRequest
8. REST API Development with Laravel Sanctum ๐
If you want to build a secure API, Laravel Sanctum provides a simple token-based authentication system.
Install Sanctum
composer require laravel/sanctum
Protect Routes
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Sanctum is great for SPAs, mobile apps, and simple APIs.
9. File Uploads and Storage ๐
Uploading files like images, PDFs, etc., is easy with Laravel.
Store File
$path = $request->file('avatar')->store('avatars');
Storage Config
Laravel has local, public, and cloud disks configured in config/filesystems.php
.
You can access files via:
Storage::url($path);
10. Pagination and Query Optimization ๐
Laravel makes pagination super simple.
Basic Pagination
$products = Product::paginate(10);
Display Pagination Links (Blade)
{{ $products->links() }}
Query Optimization
- Use Eager Loading to avoid N+1 queries:
$orders = Order::with('products')->get();
- Use Indexes in your database for faster queries.
- Use select() to fetch only needed columns.
Final Thoughts ๐ก
Laravel has so much to offer, and this checklist just scratches the surface. As a beginner, focus on understanding these core concepts before diving into more advanced features.