Laravel 5.2 - Service Injection in view template with example

Laravel 5.2 - Service Injection in view template with example

Laravel 5.2 - Service Injection in view template with example

In this tutorial, I am going to tell you how to inject service directly into your views.

There are various situation where you need to use this technique.

@inject directive is helpful directive to retrieve a service from the Laravel service container.

You will have to pass two argument in @inject, first is variable name and second argument is your service or interface class name.

Imaginate you have some kind of class in your system, for example i have a class that return total number of category on the site.

So for this, we have created a class called 'Stats.php' that have a function to return total number of category.

app/Stats.php
  1. <?php
  2. namespace App;
  3. class Stats
  4. {
  5. public function totalCategory(){
  6. return 10;
  7. //you can write here your db query as per your need.
  8. }
  9. }

Ok so we have service class here and now let's imaginate we want to use this with our view.

As we know on fresh installation, we have welcome view so we are going to start there.

In bottom of welcome.blade.php, we include a view file called stats.blade.php.

  1. <body>
  2. <div class="container">
  3. <div class="content">
  4. <div class="title">Laravel 5</div>
  5. </div>
  6. </div>
  7. @include('stats')
  8. </body>

So create a view stats.blade.php in following path resources/views/.

Example 1 to inject service method in your view using @inject directive

This is first example to inject service in view.

resources/views/stats.blade.php
  1. @inject('stats', 'App\Stats')
  2. <div>
  3. Total Category: {{ $stats->totalCategory() }}.
  4. </div>

Example 2 to inject service method in your view from routes file

resources/views/stats.blade.php
  1. <div>
  2. Total Category: {{ $stats->totalCategory() }}
  3. </div>
app/Http/routes.php
  1. Route::get('/', function (App\Stats $stats) {
  2. return view('welcome',compact('stats'));
  3. });

This was a pretty example where we pass objects of Stats class in view.

Example 3 to inject service method using view composer

If you have an any experience with Laravel you know the one solution is to create a view composer. When you composing this view with view portion then it has set of data.

resources/views/stats.blade.php
  1. <div>
  2. Total Category: {{ $stats->totalCategory() }}
  3. </div>
app/Http/routes.php
  1. View::composer('stats',function($view){
  2.     $view->with('stats',app('App\Stats'));
  3. });

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort sapanca escort karasu escort hendek escort ankara escort
sakarya escort kızlar sakarya escort kaliteli sakarya escort vip sakarya escort elit sakarya escort bayan sakarya escort adresleri.