Search code examples
phplaravellaravel-package

Accessing css with asset() in laravel package


Eghad, the laravel documentation is lacking. So if you want to have css in a package for the package views specifically, and you figure out how to publish those assets, how do you specify the path when calling them with the asset() helper?

e.g.:

I have a packages with a resources/assets/css/mystyles.css I tell the Service Provider to publish my assets under 'vendor/myorg/mypackage'

// Publishing assets.
$this->publishes([
    __DIR__.'/../resources/assets' => public_path('vendor/myorg/mypackage'),
], 'mypackage.assets');

I have a view in my package where I want it to display that css, what do I put in the asset() call? 'css/mystyles.css'? 'vendor/myorg/mypackage/css/mystyles.css'? something even more convoluted?

<link href="{{ asset('????/css/mystyles.css') }}" rel="stylesheet" />

Solution

  • Read here

    Your package user must publish your assets. They will be placed at (in your case) to vendor/myorg/mypackage - you specify that patch at provider.

    After publishing you can reach asset by asset('vendor/myorg/mypackage/css/mystyles.css')