To include and render JavaScript and CSS assets in Blade templates in Laravel, you can follow these steps:
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.
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">
npm install laravel-mix --save-dev
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');
npm run dev
command to compile your assets:npm run dev
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.