"Service Injection" of Laravel 5.2
<p>About "Service Injection",Documentation says:    <a href="https://laravel.com/docs/5.2/blade#service-injection" rel="nofollow">https://laravel.com/docs/5.2/blade#service-injection</a>   </p>

<blockquote>
  <h1>Service Injection</h1>
  
  <p>The @inject directive may be used to retrieve a service from the Laravel service container. The first argument passed to @inject is the name of the variable the service will be placed into, while the second argument is the class / interface name of the service you wish to resolve:   </p>
</blockquote>

<pre><code>@inject('metrics', 'App\Services\MetricsService')
&lt;div&gt;
    Monthly Revenue: {{ $metrics-&gt;monthlyRevenue() }}.
&lt;/div&gt;  
</code></pre>

<h1>My code:</h1>

<p>1、Service:<br>
NameAndRole.php</p>

<pre><code>class NameAndRole
{
    public function nameAndRole() {
        $user = \Auth::user();
        $username= $user-&gt;name;
        $role =$user-&gt;getRoles();
        $slug=$role[0]['slug'];
        return compact('username','slug');
    }
}
</code></pre>

<p>2、view:<br>
sidebar.blade.php</p>

<pre><code>@inject('nameAndRole','App\Services\NameAndRole')
&lt;div&gt;
  &lt;h4 &gt;username：{{$username}}&lt;/h4&gt;
  &lt;h4&gt;slug：{{$slug}}&lt;/h4&gt;
&lt;/div&gt;
</code></pre>

<p><strong>error:</strong></p>

<pre><code>Undefined variable: username (View: D:\wnmp\www\laravel-book\resources\views\partials\sidebar.blade.php) (View: D:\wnmp\www\laravel-book\resources\views\partials\sidebar.blade.php)
</code></pre>