2

In my laravel app I have app.blade.php with following code

<html>
    <body>
        <div>
            @yield('content')
        </div>
        <script src="{{ asset('jquery.min.js') }}" ></script>
        <script src="{{ asset('bootstrap.min.js') }}" ></script>
    </body>
</html>

I have several pages such as page1, page2 etc and each page is rendering as page1.blade.php etc and these pages has separate javascript files like page1.js, page2.js.

page1.js should be included with page1.blade.php only and same for page2.js also. How can I include js files for the corresponding blade files?

My page1.blade.php is

@extends('layouts.app')

@section('content')
<section class="content-header">
  <h1>
    Page 1
  </h1>

</section>
@endsection

2 Answers 2

5

app.blade.php

<html>
    <body>
        <div>
            @yield('content')
        </div>
        <script src="{{ asset('jquery.min.js') }}" ></script>
        <script src="{{ asset('bootstrap.min.js') }}" ></script>
        @yield('script')
    </body>
</html>

and page1.blade.php

@extends('layouts.app')

@section('content')
<section class="content-header">
  <h1>
    Page 1
  </h1>

</section>
@endsection

@section('script')
  <script src="{{ asset('page1.js') }}" ></script>
@endsection
Sign up to request clarification or add additional context in comments.

Comments

1

You can use @stack method that comes after 5.1 versions. For Details: https://laravel.com/docs/5.7/blade#stacks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.