Back to PHP tutorials
Intermediate19 min read

Laravel Framework

Build applications with Laravel routing, Eloquent ORM, Blade templates, and middleware.

Routing and Controllers

Routes in routes/web.php and routes/api.php map URIs to controllers or closures. Route parameters {id} inject into methods. Route groups apply middleware and prefixes.

Controllers organize request handling; invokable controllers suit single-action endpoints. Form requests validate input before controller bodies run.

Named routes power URL generation in Blade with route() helper avoiding hard-coded paths.

  • Use apiResource for RESTful route sets
  • Keep controllers thin; delegate to actions/services
  • Cache routes in production with route:cache
Route::middleware('auth')->group(function () {
    Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
});

Eloquent ORM

Eloquent models map tables to classes with relationships hasMany, belongsTo, and belongsToMany. Migrations version schema; factories seed tests.

Eager load with with() to prevent N+1 queries. Accessors and mutators transform attributes; casts handle JSON and datetime columns.

Use query builder DB::table when raw performance beats active record patterns for reports.

  • Mass assignment protect with $fillable or $guarded
  • Use database transactions for checkout flows
  • Index foreign keys used in relationship queries
class Order extends Model {
    protected $casts = ['placed_at' => 'datetime'];

    public function items(): HasMany {
        return $this->hasMany(OrderItem::class);
    }
}

Blade and Middleware

Blade templates compile to PHP with @if, @foreach, and components for reusable UI. Layouts yield sections with @extends and @section.

Middleware filters requests: auth, throttle, VerifyCsrfToken. Register in bootstrap/app.php or HTTP kernel depending on Laravel version.

Queues and jobs offload email and webhooks to horizon workers asynchronously.

  • Escape output with {{ }} automatically; {!! !!} only when safe
  • Use Vite integration for frontend assets
  • Test middleware with HTTP feature tests

Get In Touch


Ready to discuss your next project? Drop me a message.