95

I am Using Laravel 5.0 , the Form and Html Helper are removed from this version , i dont know how to include external css and js files in my header file. Currently i am using this code.

<link rel="stylesheet" href="/css/bootstrap.min.css"> <!---  css file  --->
<script type="text/javascript" src="/js/jquery.min.js"></script>
1
  • 1
    could you mark the top answer as the answer? As it is valid information and works like you asked. Commented Feb 3, 2019 at 22:41

16 Answers 16

140

I think that the right way is this one:

<script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script>

Here I have a js directory in the laravel's app/public folder. There I have a jquery.js file. The function URL::asset() produces the necessary url for you. Same for the css:

<link rel="stylesheet" href="{{ URL::asset('css/somestylesheet.css') }}" />

Keep in mind that the old mehods:

{{ Form::script() }}

and

{{ Form::style() }}

are deprecated and will not work in Laravel 5!

Sign up to request clarification or add additional context in comments.

5 Comments

Also note that the assets directory in Laravel 5 is not associated with this facade. That confused me for a second, URL::asset will literally point to your public folder.
Yes that's right. You can also use the asset() helper method. It does the same work.
Why do you not simply write src="/js/jquery.js" and href="/css/somestylesheet.css" ?
I normally use asset() and it works. Can someone explain the difference?
the major difference is, asset() works even when you access from sub-directory where as adding src="/js/jquery.js" doesn't work as it always expects files at root level and those js files will not be found hence 404s
36

Try it.

You can just pass the path to the style sheet .

{!! HTML::style('css/style.css') !!}

You can just pass the path to the javascript.

{!! HTML::script('js/script.js') !!}

Add the following lines in the require section of composer.json file and run composer update "illuminate/html": "5.*".

Register the service provider in config/app.php by adding the following value into the providers array:

'Illuminate\Html\HtmlServiceProvider'

Register facades by adding these two lines in the aliases array:

'Form'=> 'Illuminate\Html\FormFacade', 'HTML'=> 'Illuminate\Html\HtmlFacade'

6 Comments

The HTML and Form Classes are removed from Laravel 5
I have added the following but this is printing the link on webpage as <link media="all" type="text/css" rel="stylesheet" href="localhost/myapp/public/css/test.css">
this solved my problem {!! HTML::style('public/css/bootstrap.css') !!}
Active maintenance on the HTML package has been taken over by the Laravel Collective. laravelcollective.com/docs/5.0/html
where exactly do we have to write this, {!! HTML::script('js/script.js') !!}, i have js connection problem
|
24

In laravel 5.1,

 <link rel="stylesheet" href="{{URL::asset('assets/css/bootstrap.min.css')}}">
 <script type="text/javascript" src="{{URL::asset('assets/js/jquery.min.js')}}"></script>

Where assets folder location is inside public folder

Comments

17

i use this way, don't forget to put your css file on public folder.

for example i put my bootstrap css on

"root/public/bootstrap/css/bootstrap.min.css"

to access from header:

<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" >

hope this help

Comments

10

Place your assets in public directory and use the following

URL::to()

example

<link rel="stylesheet" type="text/css" href="{{ URL::to('css/style.css') }}">
<script type="text/javascript" src="{{ URL::to('js/jquery.min.js') }}"></script>

Comments

7

At first, you have to put your .css or .js files in the public folder of your project. You may create subfolders to it and move the files into the subfolder. Then you have to do the followings

<link rel="stylesheet" href="{{asset('css/your_css_file.css')}}">
<script src="{{asset('js/your_js_file.js')}}"></script>

In my example, I have created two subfolders called css and js. I put all of the .css and .js files in corresponding folders and use them where ever I want them. Thank you and hope this helps you.

Comments

7

There are many ways to include external css and js files as specified in below code but you can choose one of them, i have tested them all, they work the same.

<link rel="stylesheet" href="{{ asset('css/stylesheet.css') }}" /><!--Recommended-->
<link rel="stylesheet" href="{{ URL::asset('css/somestylesheet.css') }}" />
<link rel="stylesheet" href="{{ URL::to('css/otherstylesheet.css') }}" />
<link rel="stylesheet" href="{{ url('css/anotherstylesheet.css') }}" />

Maybe there are more ways to load it.

but keep in mind that if you are loading the css and js from other than a public folder then better to use Laravel mix.

Comments

6
{{ HTML::style('yourcss.css') }}
{{ HTML::script('yourjs.js') }}

3 Comments

how come? did you try using URL::asset() or app_path() ?
this solved my problem thanks {{ asset('public/css/bootstrap.css') }}
Instead of HTML, it needs to be Html.
4

Laravel 5.5 comes with mix , the replacement for Elixir, the external css(sass) can be extracted to resources/assets/css(sass) and import to app.css(scss) or give the correct path to your node_module folder that contains the library and can be used as

<link rel="stylesheet" href="{{ mix('css/app.css') }}" >

The same can be done for Javascript too .

2 Comments

I'm not sure, but I think it only works if you use Mix compiled resources (after generating the versioned file). (laravel.com/docs/5.5/mix#sass)
using <link rel="stylesheet" href="{{ mix('path/file') }}" > is the new way on 5.5 laravel.com/docs/5.5/mix#versioning-and-cache-busting
3

Ok so I was able to figure out for the version 5.2. You will first need to create assets folder in your public folder then put css folder and all css files into it then link it like this :

<link href="assets/css/bootstrap.min.css" rel="stylesheet">

Hope that helps!

1 Comment

My CSS file was not being linked to in my blade view. The problem was I had put it in /resources/assets/css/ with the blade views, but the solution was I needed to put it in /public/assets/css/
3

CORRECT & SECURED WAY


If you have a external .css or .js file to add into your page!

My version : Laravel Framework 5.7

If you want to add a External .js file..

  1. Copy your External codes.
  2. Run command npm install in your terminal.
  3. When the above command is executed you can see a folder named node_modules.
  4. Then go to resources/js.
  5. Open file app.js.
  6. Scroll down to the bottom and paste your External JS.
  7. Finally run command npm run dev in your terminal.

your scripts will be updated and its compiled and moved to public/js/app.js . by adding a .js file in public folder will not effect on your page.


If you want to add a External .css file..

  1. Copy your External styles.
  2. Run command npm install in your terminal.
  3. When the above command is executed you can see a folder named node_modules.
  4. Then go to resources/sass.
  5. Open file app.scss.
  6. Scroll down to the bottom and paste your External Styles.
  7. Finally run command npm run dev in your terminal.

your scripts will be updated and its compiled and moved to public/css/app.css . by adding a .css file in public folder will not effect on your page.

  • Easiest and the safest way to add your external .css and .js.

Laravel JavaScript & CSS Scaffolding

YouTube Compiling Assets

Comments

2

First you have to install the Illuminate Html helper class:

composer require "illuminate/html":"5.0.*"

Next you need to open /config/app.php and update as follows:

'providers' => [

...

Illuminate\Html\HtmlServiceProvider::class,
],

'aliases' => [

...

'Form' => Illuminate\Html\FormFacade::class, 
'HTML' => Illuminate\Html\HtmlFacade::class,
],

To confirm it’s working use the following command:

php artisan tinker
> Form::text('foo')
"<input name=\"foo\" type=\"text\">"

To include external css in you files, use the following syntax:

{!! HTML::style('foo/bar.css') !!}

2 Comments

He said: " Form and Html Helper are removed from this version". He doesn't want to use HTML Helper though..
This is a way to get that support back in Laravel 5.0 and thus enabling him to use a syntax he is accustomed to, since he specifically states that the support is removed and he don't know of another way to include external files.
1

I may be late to the party but it's easy to include JS and CSS the Laravel Way by simply using asset() function

e.g. <link rel="stylesheet" href="{{ asset('css/app.css') }}" />

Hope that helps

Comments

1

I have been making use of

<script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script> 
for javascript and 
     <link rel="stylesheet" href="{{ URL::asset('css/main.css') }}">
for css, this points to the public directory, so you need to keep your css and js files there.

Comments

1
 composer require laravelcollective/html

This command will add laracollective/html package in your project,which is very useful in form creating.After that use

{!! Html::style('public/css/bootstrap.min.css') !!}

{!! Html::script('public/js/jquery.min.js') !!}
{!! Html::script('public/js/bootstrap.min.js') !!}

Comments

0

It mostly happens due to laravel webpackmix is not yet executed

composer require laravel/ui PHP artisan ui vue --auth //you can use vue boostrap --auth is to add the login paths and

npm install if it's asking to run "npm fund" then do it npm run dev

try this once again

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.