Laravel Displaying Data

Display data that is passed to our Blade views by enclosing the variable in curly braces.

The following route is given below:

Route::get('greeting', function () {
return view('welcome', ['name' => 'rafia']);
});

We display the contents of the name variable like:

Hii, {{ $name }}
  • Blade {{ }} statements are sent through PHP`s htmlspecialchars function. It prevents XSS attacks.

We can put any PHP code where we wish to insert a Blade echo statement:

The current UNIX timestamp is {{ time( ) }}.

Displaying Unescaped Data

Blade {{ }} statements are automatically sent through PHP`s htmlspecialchars function to prevent XSS attacks.

If we need to escape our data, we have to  use the following syntax:

Hello, {!! $name !!}.

Rendering JSON

We pass an array to our view with the intention of rendering (providing) it as JSON to initialize a JavaScript variable.

For example:

 <script>
 var app = <?php echo json_encode($array); ?>;
 </script> 

However, instead of manually calling json_encode, we can use the @json Blade directive. The @json directive accepts the same arguments as PHP`s json_encode function do: 

 <script>
 var app = @json($array);
 var app = @json($array, JSON_PRETTY_PRINT);
 </script> 

The @json directive is also useful for seeding Vue components or data-* attributes:

<example-component :some-prop=’@json($array)’></example-component>
  • Using the @json in element attributes requires to be surrounded by single quotes.

HTML Entity Encoding

Blade ( and the Laravel e helper ) will double encode HTML entities. If we would like to disable double encoding, call the Blade::withoutDoubleEncoding() method from the boot method of our AppServiceProvider:

 <?php
 namespaceApp\Providers;
 useIlluminate\Support\Facades\Blade;
 useIlluminate\Support\ServiceProvider;
 classAppServiceProviderextendsServiceProvider
 {
 /**
      * Bootstrap any application services.
      *
      * @return void 
      */
 publicfunctionboot()
 {
 Blade::withoutDoubleEncoding();
 }
 } 

Blade & JavaScript Frameworks

JavaScript frameworks also use “curly” braces, which indicate that the given expression is displayed in the browser.

?We use @ symbol to inform the Blade, providing engine an expression that should remain untouched.

For example:

<h3> Laravel </h3>
 Hii, @{{ name }}.  

In the above example, @ (at the rate) symbol will be removed by the Blade.

However, {{ name }} expression will not be touched by the Blade engine.

The @verbatim Directive

If we are displaying JavaScript variables in a large portion of our template,

?we wrap the HTML in the @verbatim Directive. It is because we do not need to prefix each Blade echo statement within the @ symbol:

 @verbatim
 <div class="container">
         Hello,{{ name }}.
 </div>
 @endverbatim 

For example:

 @verbatim
 Name of javascript {{ name }}
 Type of application {{ type }}
 <!- - and few more like these -->
 @endverbatim
 </div>

Related Topics

Laravel Tutorial

Laravel Tutorial is a PHP based web-framework; it is used for developing high-end web applications by using significant syntax's. It has a substantial collection of tools and provides application architecture....

5 minutes read.

Laravel Templating Inheritance

Introduction: The Blade is a PHP Templating Engine. It grabs the PHP code and makes it easy for us to implement in HTML. The Blade is a simple, and powerful Templating Engine provided by...

4 minutes read.

Laravel Advantages

Top 8 Advantages of Laravel The following advantages that Laravel offers, it is based upon designing a web application:- 1. Powerful Authentication: The Laravel PHP framework was developed with a purpose that can help...

2 minutes read.

Features of Laravel Framework

Top 8 Laravel Features Laravel has a very rich set of features that makes the speed of web development faster. The following features that serve the Laravel`s key points: Bundles: Bundles provide us a Modular Packaging...

2 minutes read.

Laravel Displaying Data

Display data that is passed to our Blade views by enclosing the variable in curly braces. The following route is given below: Route::get('greeting', function () { return view('welcome', ['name' => 'rafia']); }); We display...

2 minutes read.

Laravel Basic Routing

Basic Routing The basic Laravel routes accept a URI and a Closure, providing a straightforward method for defining routes: Route::get('/', function () { return view('welcome') }); E.g.: Route::get('/', function () { return "Hello Laravel"; }); Output: We...

3 minutes read.

Laravel Controller Middleware

Controller Middleware Middleware can be assigned to the controller`s routes in our route files: Route::get(‘profile’, ‘UserController@show’) ->middleware(‘auth’); It is more convenient to specify middleware within our controller`s constructor. Using the middleware method from our controller`s constructor,...

1 minute read.

Laravel Named Routes

Named Routes Named routes allow the suitable generation of URLs or redirects to specific routes. We specify a name for a route by changing the name method onto the route definition: Syntax:  Route::get(‘user/profile’, function () {...

2 minutes read.

Raw SQL Queries

Raw SQL Queries: Laravel interact with databases by the variety of database back-ends with raw SQL, the fluent query builder, and the Eloquent ORM. Laravel supports four types of databases: MySQLPostgreSQLSQLiteSQL Server Configuration The database...

3 minutes read.

CSS Icons

CSS Icons       CSS icons are specified like a symbol or image used inside a computer interface assign to any element. CSS icons are the graphical depiction of any program or...

4 minutes read.

Laravel Control Statements

Blade also provides convenient shortcuts for common control structure in PHP, such as conditional statements and loops. These shortcuts provide a very clean working with PHP structures, while also remain familiar to...

3 minutes read.

Laravel Uploading Files

Retrieving Uploaded Files We can access files from an Illuminate\Http\Request. By using the file method or using the dynamic properties. The file method returns an instance of the Illuminate\Http\UploadedFile class that extends the...

2 minutes read.

Laravel Authentication

Authentication is a process of identifying user credentials. Laravel makes implementing authentication very simple. The authentication configuration file is located at config/auth.php that contains various documented options for adjusting the behavior of the authentication...

4 minutes read.

Laravel Eloquent Database Relationships

Eloquent Relationships Laravel: Database tables are related to one another. Eloquent manages and work with easy relationships. It supports some different types of relationship, which are as follows: One to oneOne to manyMany...

8 minutes read.

Laravel First Packages

First Packages  Laravel provides the following ready to use packages: Cashier: It was introduced in Laravel 4.2 version, it provides an interface for managing subscription billing service, like handling coupons and generating invoices. SSH: It was...

2 minutes read.

Laravel vs Other Frameworks

Laravel is one of the top listing PHP framework. Some of the reasons which make Laravel top listing PHP framework that are as follows: Authorization Technique.Object-Oriented Libraries.Artisan.MVC Support.Security.Database Migration.Database Template Engine. In this Pie Chart distribution, we...

6 minutes read.

Laravel Resource Controllers

Laravel resource routing assigns the “CRUD” routes to a controller with the help of single line code. For E.g., If we wish to create a controller that handles all HTTP requests “photos” stored by...

4 minutes read.

Laravel Data Views

Passing the Data Views The data should be an array with the key/value pairs while passing the information in the following (return view(‘greeting’, [‘name’ => ‘John’]);) manner. Inside the view, we...

4 minutes read.

Laravel Route Parameters

Required Parameters: We need to capture segments of the URI within our route. We are going to see how to pass parameters through two or many views inside the closure function. Route::get('/home',...

2 minutes read.

Laravel Forms

CSRF Field For defining an HTML form in our application, we should include a hidden CSRF token field in the form, so that the CSRF protection middleware can validate the request. We use the...

3 minutes read.