3

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.

1

3 Answers 3

3

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

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

Comments

2

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

Can you elaborate on this a little more? I'm trying to do the same thing using 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.
just add a readme for vendor:publish.
1

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">

1 Comment

Thanks this is what i was looking for.

Your Answer

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