laravel blade php code
How to write PHP code inside of Laravel Blade template?
I am writing code for a site that was done in Laravel, I know that it is not recommended to write PHP code within the Blade template, but in this instance I have limited time available.
The PHP code in the middle does not work?
2 Answers 2
(Here better implement the action in the controller, you can make a helper class, bind it through a service provider, and use it)
In some situations, it’s useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:
which is exactly the same as:
If you need to share global variables best way to do it is to use the method share() of the View facade, and do that on a service provider. https://laravel.com/docs/5.7/views#passing-data-to-views
ex of use case: nested structure for layouts => save global variables with short names, that point to bigger name of tree traversal (+ easy to maintain when we alter the structure, do it once). ex:
Here i created a new ServiceProvider, you can use too existing one, like the AppServiceProvider.
If the action to execute is depending on the controller calling, better implement things on controllers, if it’s view specific (internal logic). Then @php @endphp is a nice way to achieve that.
Also for sharing variable Know that we can simply use @php @phpend in a layout view (it will be available on all included partials and object that extend it). So you know the nature of @include and @yield,@extend on that matter. However that’s not the best practice or recommended. What’s recommended is to use the helper View::share() above.
The other way to write code is to create your own directive:
(note all you need is to create one function on a service provider, and you have your own directive, )
And What about sharing function globally? Create and Use a global helper. By creating a file that will contain all the helpers (functions). Then either use the composer** auto loading**. Or bind an include_once through a service provider.
Laravel Framework Russian Community
Prologue
Getting Started
Architecture Concepts
The Basics
Frontend
Security
Digging Deeper
Database
Eloquent ORM
Testing
Official Packages
Blade Templates
Introduction
Template Inheritance
Defining A Layout
Two of the primary benefits of using Blade are template inheritance and sections. To get started, let’s take a look at a simple example. First, we will examine a «master» page layout. Since most web applications maintain the same general layout across various pages, it’s convenient to define this layout as a single Blade view:
As you can see, this file contains typical HTML mark-up. However, take note of the @section and @yield directives. The @section directive, as the name implies, defines a section of content, while the @yield directive is used to display the contents of a given section.
Now that we have defined a layout for our application, let’s define a child page that inherits the layout.
Extending A Layout
When defining a child view, use the Blade @extends directive to specify which layout the child view should «inherit». Views which extend a Blade layout may inject content into the layout’s sections using @section directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield :
In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout’s sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.
Blade views may be returned from routes using the global view helper:
Components & Slots
Components and slots provide similar benefits to sections and layouts; however, some may find the mental model of components and slots easier to understand. First, let’s imagine a reusable «alert» component we would like to reuse throughout our application:
Sometimes it is helpful to define multiple slots for a component. Let’s modify our alert component to allow for the injection of a «title». Named slots may be displayed by simply «echoing» the variable that matches their name:
Passing Additional Data To Components
Sometimes you may need to pass additional data to a component. For this reason, you can pass an array of data as the second argument to the @component directive. All of the data will be made available to the component template as variables:
Displaying Data
You may display data passed to your Blade views by wrapping the variable in curly braces. For example, given the following route:
You may display the contents of the name variable like so:
Of course, you are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:
Blade << >> statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks.
Displaying Unescaped Data
By default, Blade << >> statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
Blade & JavaScript Frameworks
Since many JavaScript frameworks also use «curly» braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched. For example:
In this example, the @ symbol will be removed by Blade; however, << name >> expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.
The @verbatim Directive
If you are displaying JavaScript variables in a large portion of your template, you may wrap the HTML in the @verbatim directive so that you do not have to prefix each Blade echo statement with an @ symbol:
Control Structures
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures, while also remaining familiar to their PHP counterparts.
If Statements
For convenience, Blade also provides an @unless directive:
In addition to the conditional directives already discussed, the @isset and @empty directives may be used as convenient shortcuts for their respective PHP functions:
Loops
In addition to conditional statements, Blade provides simple directives for working with PHP’s loop structures. Again, each of these directives functions identically to their PHP counterparts:
When looping, you may use the loop variable to gain valuable information about the loop, such as whether you are in the first or last iteration through the loop.
When using loops you may also end the loop or skip the current iteration:
You may also include the condition with the directive declaration in one line:
The Loop Variable
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iteration remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent | When in a nested loop, the parent’s loop variable. |
Comments
Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:
In some situations, it’s useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:
While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template.
Including Sub-Views
Blade’s @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:
Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
Of course, if you attempt to @include a view which does not exist, Laravel will throw an error. If you would like to include a view that may or may not be present, you should use the @includeIf directive:
If you would like to @include a view depending on a given boolean condition, you may use the @includeWhen directive:
You should avoid using the __DIR__ and __FILE__ constants in your Blade views, since they will refer to the location of the cached, compiled view.
Rendering Views For Collections
You may combine loops and includes into one line with Blade’s @each directive:
You may also pass a fourth argument to the @each directive. This argument determines the view that will be rendered if the given array is empty.
Stacks
Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. This can be particularly useful for specifying any JavaScript libraries required by your child views:
You may push to a stack as many times as needed. To render the complete stack contents, pass the name of the stack to the @stack directive:
Service Injection
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 or interface name of the service you wish to resolve:
Extending Blade
Blade allows you to define your own custom directives using the directive method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.
As you can see, we will chain the format method onto whatever expression is passed into the directive. So, in this example, the final PHP generated by this directive will be:
After updating the logic of a Blade directive, you will need to delete all of the cached Blade views. The cached Blade views may be removed using the view:clear Artisan command.
How to Format Laravel Blade Codes in Visual Studio Code?
I have installed some Visual Studio Code extensions to handle Laravel Blade Codes like
But they are not helping me to format Laravel Blade Codes [blade.php files]. I mean they are not auto indenting the codes as I expected. But I have seen blade codes online which are well indented in visual studio Code IDE.
Example :
Is their any specific configuration or technique which I can use to make these extensions work or there is some other way? Thanks in advance
12 Answers 12
First go to «Visual Studio Code» Settings and search for the option «files.associations» If that option is not available in your settings. Please update your Visual Studio Code to latest version.
In the settings overwrite panel past the following snippet
Save it and your are good to go. Happy formatting 🙂
Update: There are still some issues in formatting html with blade code, the formatter tend to bring blade codes in a single line while executing format command. However, if your codes grow that’s won’t be a problem. Moreover, you can easily manage those by putting a line comment in between your blade blocks. And commenting in code is always a good thing.
Update: Seems updated version adds an option to enable blade formatting in the user settings. To use that,
Please make sure you are using the latest version and «Laravel Blade Snippets» Extension installed.
Then Ctrl + Shift + P : type User settings : Enter
3. In «User settings» > «Extensions» > «Blade Configaration» check the option Enable format blade file.
Blade Templates
Introduction
Blade views may be returned from routes or controller using the global view helper. Of course, as mentioned in the documentation on views, data may be passed to the Blade view using the view helper’s second argument:
Before digging deeper into Blade, make sure to read the Laravel view documentation.
Displaying Data
You may display data that is passed to your Blade views by wrapping the variable in curly braces. For example, given the following route:
You may display the contents of the name variable like so:
Blade’s << >> echo statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks.
You are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:
Rendering JSON
Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. For example:
You should only use the @json directive to render existing variables as JSON. The Blade templating is based on regular expressions and attempts to pass a complex expression to the directive may cause unexpected failures.
HTML Entity Encoding
By default, Blade (and the Laravel e helper) will double encode HTML entities. If you would like to disable double encoding, call the Blade::withoutDoubleEncoding method from the boot method of your AppServiceProvider :
Displaying Unescaped Data
By default, Blade << >> statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Be very careful when echoing content that is supplied by users of your application. You should typically use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
Blade & JavaScript Frameworks
Since many JavaScript frameworks also use «curly» braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched. For example:
In this example, the @ symbol will be removed by Blade; however, << name >> expression will remain untouched by the Blade engine, allowing it to be rendered by your JavaScript framework.
The @ symbol may also be used to escape Blade directives:
The @verbatim Directive
If you are displaying JavaScript variables in a large portion of your template, you may wrap the HTML in the @verbatim directive so that you do not have to prefix each Blade echo statement with an @ symbol:
Blade Directives
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.
If Statements
For convenience, Blade also provides an @unless directive:
In addition to the conditional directives already discussed, the @isset and @empty directives may be used as convenient shortcuts for their respective PHP functions:
Authentication Directives
The @auth and @guest directives may be used to quickly determine if the current user is authenticated or is a guest:
If needed, you may specify the authentication guard that should be checked when using the @auth and @guest directives:
Environment Directives
You may check if the application is running in the production environment using the @production directive:
Or, you may determine if the application is running in a specific environment using the @env directive:
Section Directives
You may determine if a template inheritance section has content using the @hasSection directive:
You may use the sectionMissing directive to determine if a section does not have content:
Switch Statements
Loops
In addition to conditional statements, Blade provides simple directives for working with PHP’s loop structures. Again, each of these directives functions identically to their PHP counterparts:
When looping, you may use the loop variable to gain valuable information about the loop, such as whether you are in the first or last iteration through the loop.
When using loops you may also end the loop or skip the current iteration using the @continue and @break directives:
You may also include the continuation or break condition within the directive declaration:
The Loop Variable
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iterations remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->even | Whether this is an even iteration through the loop. |
$loop->odd | Whether this is an odd iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent | When in a nested loop, the parent’s loop variable. |
Conditional Classes
The @class directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:
Including Subviews
While you’re free to use the @include directive, Blade components provide similar functionality and offer several benefits over the @include directive such as data and attribute binding.
Blade’s @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:
Even though the included view will inherit all data available in the parent view, you may also pass an array of additional data that should be made available to the included view:
If you attempt to @include a view which does not exist, Laravel will throw an error. If you would like to include a view that may or may not be present, you should use the @includeIf directive:
To include the first view that exists from a given array of views, you may use the includeFirst directive:
You should avoid using the __DIR__ and __FILE__ constants in your Blade views, since they will refer to the location of the cached, compiled view.
Rendering Views For Collections
You may combine loops and includes into one line with Blade’s @each directive:
You may also pass a fourth argument to the @each directive. This argument determines the view that will be rendered if the given array is empty.
Views rendered via @each do not inherit the variables from the parent view. If the child view requires these variables, you should use the @foreach and @include directives instead.
The @once Directive
The @once directive allows you to define a portion of the template that will only be evaluated once per rendering cycle. This may be useful for pushing a given piece of JavaScript into the page’s header using stacks. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript to the header the first time the component is rendered:
Raw PHP
In some situations, it’s useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:
Comments
Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:
Components
Components and slots provide similar benefits to sections, layouts, and includes; however, some may find the mental model of components and slots easier to understand. There are two approaches to writing components: class based components and anonymous components.
To create a class based component, you may use the make:component Artisan command. To illustrate how to use components, we will create a simple Alert component. The make:component command will place the component in the App\View\Components directory:
The make:component command will also create a view template for the component. The view will be placed in the resources/views/components directory. When writing components for your own application, components are automatically discovered within the app/View/Components directory and resources/views/components directory, so no further component registration is typically required.
You may also create components within subdirectories:
The command above will create an Input component in the App\View\Components\Forms directory and the view will be placed in the resources/views/components/forms directory.
Manually Registering Package Components
When writing components for your own application, components are automatically discovered within the app/View/Components directory and resources/views/components directory.
However, if you are building a package that utilizes Blade components, you will need to manually register your component class and its HTML tag alias. You should typically register your components in the boot method of your package’s service provider:
Once your component has been registered, it may be rendered using its tag alias:
Alternatively, you may use the componentNamespace method to autoload component classes by convention. For example, a Nightshade package might have Calendar and ColorPicker components that reside within the Package\Views\Components namespace:
This will allow the usage of package components by their vendor namespace using the package-name:: syntax:
Blade will automatically detect the class that’s linked to this component by pascal-casing the component name. Subdirectories are also supported using «dot» notation.
Rendering Components
To display a component, you may use a Blade component tag within one of your Blade templates. Blade component tags start with the string x- followed by the kebab case name of the component class:
Passing Data To Components
You may pass data to Blade components using HTML attributes. Hard-coded, primitive values may be passed to the component using simple HTML attribute strings. PHP expressions and variables should be passed to the component via attributes that use the : character as a prefix:
You should define the component’s required data in its class constructor. All public properties on a component will automatically be made available to the component’s view. It is not necessary to pass the data to the view from the component’s render method:
When your component is rendered, you may display the contents of your component’s public variables by echoing the variables by name:
Casing
Escaping Attribute Rendering
Since some JavaScript frameworks such as Alpine.js also use colon-prefixed attributes, you may use a double colon ( :: ) prefix to inform Blade that the attribute is not a PHP expression. For example, given the following component:
The following HTML will be rendered by Blade:
Component Methods
In addition to public variables being available to your component template, any public methods on the component may be invoked. For example, imagine a component that has an isSelected method:
You may execute this method from your component template by invoking the variable matching the name of the method:
Accessing Attributes & Slots Within Component Classes
The closure should return a string. If the returned string corresponds to an existing view, that view will be rendered; otherwise, the returned string will be evaluated as an inline Blade view.
Additional Dependencies
If your component requires dependencies from Laravel’s service container, you may list them before any of the component’s data attributes and they will automatically be injected by the container:
Hiding Attributes / Methods
Component Attributes
Using directives such as @env within component tags is not supported at this time. For example, will not be compiled.
Default / Merged Attributes
Sometimes you may need to specify default values for attributes or merge additional values into some of the component’s attributes. To accomplish this, you may use the attribute bag’s merge method. This method is particularly useful for defining a set of default CSS classes that should always be applied to a component:
If we assume this component is utilized like so:
The final, rendered HTML of the component will appear like the following:
Conditionally Merge Classes
If you need to merge other attributes onto your component, you can chain the merge method onto the class method:
If you need to conditionally compile classes on other HTML elements that shouldn’t receive merged attributes, you can use the @class directive.
Non-Class Attribute Merging
When merging attributes that are not class attributes, the values provided to the merge method will be considered the «default» values of the attribute. However, unlike the class attribute, these attributes will not be merged with injected attribute values. Instead, they will be overwritten. For example, a button component’s implementation may look like the following:
The rendered HTML of the button component in this example would be:
If you would like an attribute other than class to have its default value and injected values joined together, you may use the prepends method. In this example, the data-controller attribute will always begin with profile-controller and any additional injected data-controller values will be placed after this default value:
Retrieving & Filtering Attributes
You may filter attributes using the filter method. This method accepts a closure which should return true if you wish to retain the attribute in the attribute bag:
For convenience, you may use the whereStartsWith method to retrieve all attributes whose keys begin with a given string:
Conversely, the whereDoesntStartWith method may be used to exclude all attributes whose keys begin with a given string:
Using the first method, you may render the first attribute in a given attribute bag:
If you would like to check if an attribute is present on the component, you may use the has method. This method accepts the attribute name as its only argument and returns a boolean indicating whether or not the attribute is present:
You may retrieve a specific attribute’s value using the get method:
Reserved Keywords
By default, some keywords are reserved for Blade’s internal use in order to render components. The following keywords cannot be defined as public properties or method names within your components:
Slots
We may pass content to the slot by injecting content into the component:
Sometimes a component may need to render multiple different slots in different locations within the component. Let’s modify our alert component to allow for the injection of a «title» slot:
Scoped Slots
Slot Attributes
Like Blade components, you may assign additional attributes to slots such as CSS class names:
To interact with slot attributes, you may access the attributes property of the slot’s variable. For more information on how to interact with attributes, please consult the documentation on component attributes:
Inline Component Views
For very small components, it may feel cumbersome to manage both the component class and the component’s view template. For this reason, you may return the component’s markup directly from the render method:
Generating Inline View Components
To create a component that renders an inline view, you may use the inline option when executing the make:component command:
Anonymous Components
Data Properties / Attributes
Since anonymous components do not have any associated class, you may wonder how you may differentiate which data should be passed to the component as variables and which attributes should be placed in the component’s attribute bag.
You may specify which attributes should be considered data variables using the @props directive at the top of your component’s Blade template. All other attributes on the component will be available via the component’s attribute bag. If you wish to give a data variable a default value, you may specify the variable’s name as the array key and the default value as the array value:
Given the component definition above, we may render the component like so:
Dynamic Components
Sometimes you may need to render a component but not know which component should be rendered until runtime. In this situation, you may use Laravel’s built-in dynamic-component component to render the component based on a runtime value or variable:
Manually Registering Components
The following documentation on manually registering components is primarily applicable to those who are writing Laravel packages that include view components. If you are not writing a package, this portion of the component documentation may not be relevant to you.
When writing components for your own application, components are automatically discovered within the app/View/Components directory and resources/views/components directory.
However, if you are building a package that utilizes Blade components or placing components in non-conventional directories, you will need to manually register your component class and its HTML tag alias so that Laravel knows where to find the component. You should typically register your components in the boot method of your package’s service provider:
Once your component has been registered, it may be rendered using its tag alias:
Autoloading Package Components
Alternatively, you may use the componentNamespace method to autoload component classes by convention. For example, a Nightshade package might have Calendar and ColorPicker components that reside within the Package\Views\Components namespace:
This will allow the usage of package components by their vendor namespace using the package-name:: syntax:
Blade will automatically detect the class that’s linked to this component by pascal-casing the component name. Subdirectories are also supported using «dot» notation.
Building Layouts
Layouts Using Components
Most web applications maintain the same general layout across various pages. It would be incredibly cumbersome and hard to maintain our application if we had to repeat the entire layout HTML in every view we create. Thankfully, it’s convenient to define this layout as a single Blade component and then use it throughout our application.
Defining The Layout Component
For example, imagine we are building a «todo» list application. We might define a layout component that looks like the following:
Applying The Layout Component
Once the layout component has been defined, we may create a Blade view that utilizes the component. In this example, we will define a simple view that displays our task list:
Now that we have defined our layout and task list views, we just need to return the task view from a route:
Layouts Using Template Inheritance
Defining A Layout
Layouts may also be created via «template inheritance». This was the primary way of building applications prior to the introduction of components.
To get started, let’s take a look at a simple example. First, we will examine a page layout. Since most web applications maintain the same general layout across various pages, it’s convenient to define this layout as a single Blade view:
As you can see, this file contains typical HTML mark-up. However, take note of the @section and @yield directives. The @section directive, as the name implies, defines a section of content, while the @yield directive is used to display the contents of a given section.
Now that we have defined a layout for our application, let’s define a child page that inherits the layout.
Extending A Layout
When defining a child view, use the @extends Blade directive to specify which layout the child view should «inherit». Views which extend a Blade layout may inject content into the layout’s sections using @section directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield :
In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout’s sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.
The @yield directive also accepts a default value as its second parameter. This value will be rendered if the section being yielded is undefined:
Forms
CSRF Field
Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the @csrf Blade directive to generate the token field:
Method Field
Validation Errors
Since the @error directive compiles to an «if» statement, you may use the @else directive to render content when there is not an error for an attribute:
You may pass the name of a specific error bag as the second parameter to the @error directive to retrieve validation error messages on pages containing multiple forms:
Stacks
Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. This can be particularly useful for specifying any JavaScript libraries required by your child views:
You may push to a stack as many times as needed. To render the complete stack contents, pass the name of the stack to the @stack directive:
If you would like to prepend content onto the beginning of a stack, you should use the @prepend directive:
Service Injection
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 or interface name of the service you wish to resolve:
Extending Blade
Blade allows you to define your own custom directives using the directive method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.
As you can see, we will chain the format method onto whatever expression is passed into the directive. So, in this example, the final PHP generated by this directive will be:
After updating the logic of a Blade directive, you will need to delete all of the cached Blade views. The cached Blade views may be removed using the view:clear Artisan command.
Custom Echo Handlers
If you attempt to «echo» an object using Blade, the object’s __toString method will be invoked. The __toString method is one of PHP’s built-in «magic methods». However, sometimes you may not have control over the __toString method of a given class, such as when the class that you are interacting with belongs to a third-party library.
In these cases, Blade allows you to register a custom echo handler for that particular type of object. To accomplish this, you should invoke Blade’s stringable method. The stringable method accepts a closure. This closure should type-hint the type of object that it is responsible for rendering. Typically, the stringable method should be invoked within the boot method of your application’s AppServiceProvider class:
Once your custom echo handler has been defined, you may simply echo the object in your Blade template:
Custom If Statements
Programming a custom directive is sometimes more complex than necessary when defining simple, custom conditional statements. For that reason, Blade provides a Blade::if method which allows you to quickly define custom conditional directives using closures. For example, let’s define a custom conditional that checks the configured default «disk» for the application. We may do this in the boot method of our AppServiceProvider :
Once the custom conditional has been defined, you can use it within your templates: