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.

  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.

  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

  1. <div>
  2. Total Category: {{ $stats->totalCategory() }}
  3. </div>
  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.

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

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort