Hello every one I am working with laravel and developing my first package and I need to add some css and javascript files with my package can any one answer me what is the right way to do this task.
-
2What have you researched so far? mention it in your question. Also if you haven't, please read up on how to ask a question and provide a minimal, complete and verifiable example that reproduces your problem.Udayraj Deshmukh– Udayraj Deshmukh2018-03-15 09:46:38 +00:00Commented Mar 15, 2018 at 9:46
3 Answers
You cannot directly add the assets from Your Laravel Package for that you have publish() assets first.
In your Service Provider
/**
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/path/to/assets' => public_path('vendor/courier'),
], 'public');
}
Then use it in your views using asset()
Hope this helps
Comments
Whenever I write Laravel packages, I strive to keep the JavaScript inline.
When I can't do that, I will use the GitHub raw URL for the JavaScript/CSS asset and load that directly in the Laravel view.
That way whenever anyone uses my Laravel packages and forgets or doesn't know about doing the php artisan vendor:publish command (~85%), it still just 'works'.
2 Comments
React and I don't want package users to publish my package. I want to deliver my assets directly from inside the package. Can you guide me to a blog post or something? Thank you.As @laravel levaral suggested you need to publish assets first afterwards in your root directory you will find public folder. Inside public you can make 'js' and 'css' folders in which you can store those files.
In order to access your assets through out the whole laravel project you just need to call it from your blade file using asset()
For js file:-
E.g:- <script src="{{ asset('js/app.js') }}"></script>
Likewise for css file:-
E.g:- <link href="{{ asset('css/app.css') }}" rel="stylesheet">