Great question! Both approaches—using @section/@yield and Blade components (<x-whatever> and $slot)—are valid and fully supported in Laravel 12. Here’s a breakdown to help you decide which to use:
1. @section and @yield
This is the traditional way to define layouts in Blade. You create a base layout and use @yield to define sections that child views can fill with @section.
Example:
layouts/app.blade.php
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
home.blade.php
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h1>Welcome Home!</h1>
@endsection
Use cases:
- Great for simple layouts and when you want to define large sections of content.
- Familiar to many Laravel developers.
2. Blade Components (<x-whatever> and $slot)
Blade components are more modern and encourage reusable, encapsulated UI pieces. You can pass data and content via attributes and slots.
Example:
resources/views/components/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{ $title ?? 'Default Title' }}</title>
</head>
<body>
{{ $slot }}
</body>
</html>
home.blade.php
<x-layout title="Home Page">
<h1>Welcome Home!</h1>
</x-layout>
Use cases:
- Ideal for reusable UI elements (alerts, cards, buttons, etc.).
- Encourages component-driven development.
- Easier to nest and compose layouts.
Which Should You Use?
- Neither is deprecated—both are fully supported.
- For new projects, Blade components are generally recommended, especially for reusable UI and layouts. They offer better encapsulation and flexibility.
- For simple layouts or legacy projects,
@section/@yieldis still perfectly fine.
In summary:
If you’re starting fresh, lean towards Blade components (<x-layout>, $slot, etc.), as they align with modern Laravel practices and make your code more maintainable and reusable. But don’t worry—both approaches are valid and will continue to be supported.
Let me know if you want more code examples or have a specific use case in mind!