How to include and render JavaScript and CSS assets in Blade templates in Laravel?

To include and render JavaScript and CSS assets in Blade templates in Laravel, you can follow these steps:

  1. Create a CSS or JavaScript file in the public folder of your Laravel project. For example, you can create a styles.css file in the public/css folder.

  2. Use the asset() or mix() helper functions in your Blade template to reference the CSS or JavaScript file. For example, to include the styles.css file, you can use the following code:

<link href="{{ asset('css/styles.css') }}" rel="stylesheet">
  1. If you have multiple CSS or JavaScript files, you can create a manifest file using Laravel Mix. This will help you combine and minify your assets. To do this, install Laravel Mix via npm:
npm install laravel-mix --save-dev
  1. Configure Laravel Mix in the webpack.mix.js file located in the root of your Laravel project. For example, you can use the following configuration to compile your CSS and JS files:
const mix = require('laravel-mix'); mix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css');
  1. Run the npm run dev command to compile your assets:
npm run dev
  1. In your Blade template, you can now include the compiled assets using the mix() helper function. For example:
<link href="{{ mix('css/app.css') }}" rel="stylesheet"> <script src="{{ mix('js/app.js') }}"></script>

By following these steps, you can include and render JavaScript and CSS assets in your Laravel Blade templates.