How to Use @inject in Blade in Laravel 5.1

Access all tutorials in sprocket icon.

June 12, 2015 from author Bill Keck.

In a previous tutorial, we looked at creating a laravel service provider for Laravel. A service provider is a beautiful aspect of the Laravel architecture, but in some cases, when you want to re-use a class in your views, you don’t need it.

Instead, you can use the @inject method in blade. It could work like this:



@section('content')

@inject('boom', 'App\Helpers\Contracts\RocketShipContract')

    {{ $boom->blastOff() }}

@endsection

Ok, so @section and @endsection are typical blade syntax to define where in the master page the ‘content’ will be placed. Then we can see we have the @inject method. The first parameter is the name of the local variable to hold the object, in this case I called it boom. You can call yours any name that falls within variable naming rules.

The next parameter is the class or contract you are instantiating. In this case, I’m calling a contract, because I have a service provider binding the contract to a concrete class. You can refer to that service provider tutorial for the exact reference.

Then in the dual curly brackets, which is again blade syntax to echo php, you see we are calling the blastOff method from the object instance variable $boom. The blastOff method simply returns a string of text. If you are curious why I chose boom, blastOff, and RocketShipContract, these were examples I was using in my service provider tutorial, which is a related subject. Check that out when you have a chance.

There’s an interesting example of @inject in a Laracast’s video where they use stats as an example, which is perhaps a closer to a real-world example.

Anyway, @inject offers you an easy alternative to service providers, allowing you to bring in reusable code that is not tied to a controller. As I’ve explained above, you can also use @inject in tandem with a service provider, when you want to call a contract instead of a concrete class.

Also note that you can place @inject outside of the @section where the object is being used. So if you want to perhaps place it at the top of the file, above the @extends line, you could do so. That might be more intuitive.


@inject('boom', 'App\Helpers\Contracts\RocketShipContract')

@extends('layouts.master')

@section('content')

    {{ $boom->blastOff() }}

@endsection

This looks really clean to me. That kind of flexibility is yet another reason to love the Laravel Framework.

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks!

I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5* For Beginners, I really appreciate it.

7 thoughts on “How to Use @inject in Blade in Laravel 5.1

  1. Md Mazedul Islam Khan says:

    Hi,
    I’ve created a custom Helper class under the App\Http\Helpers directory and I am injecting the class using the Blade @inject() injection like the following:

    @inject(‘Hello’, ‘App\Http\Helpers\Helper’)

    And, I’m referencing Hello in the same view like the following:

    Hello::activity(1, 2, 3)

    However, this returns the following error:

    Fatal error: Class ‘Hello’ not found (View: /Users/sumon/Desktop/TimeCop/resources/views/diary/view.blade.php)

    Like

    • I haven’t used a static call like that, I believe your syntax should be Hello->activity(1,2,3). The way you are doing it is like calling Hello as a class. It is not, it’s a class instance. I have not tested, but I think this will solve your problem.

      Like

  2. Md Mazedul Islam Khan says:

    I’ve fixed it. I was missing the variable. According to the docs the inject() directive creates a variable to be used in the view context, not a reference to a class.

    {{ $Hello->activity(1, 2, 3) }}

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s