Laravel @stack Directive

The @stack directive allows you to create a field with the specified name. Thus, you can add page-specific content to this field in your other views using the @push directive.

For example, you can show page-specific js, css or some other code only when you enter that page.

In your layout page, in this example mine is layouts/index.blade.php, I created a field called custom-head inside my <head> tag.

// layouts/index.blade.php
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{ config('app.name', 'Laravel') }}</title>
    ..
    @stack('custom-head')
    ..
</head>

And now, if I want to add a custom css, js or any code to that page in my subviews, we can use the @push directive.

// users.blade.php
@extends('layout')

@push('custom-head')
    <link href="{{ mix('css/users.css') }}">
@endpush

Unlike the @section and @yield directives, you can add content multiple times with these directives. However, the @section and @yield directives add one-time content.
And in addition, remember that you have to specify the page to extend with the @extends directive.

Comments

There are no comments, make the firs comment