Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to assign or declare variable in laravel blade template

How to assign or declare variable in laravel blade template ?

Aman Jain, November 8, 2022March 16, 2024

Laravel blade is a powerful templating engine that gives you a lot of control over your HTML. One thing you can declare variable in laravel blade template easily by using different methds. This can be useful if you want to reuse a piece of data in your template, or if you want to make your template more dynamic. To declare a variable in a blade template, you use the “@php” and “@endphp” symbol and can define the variable as normal .

Declare variable in laravel blade template using @php syntax

For example, to declare a variable called “name”, you would use the following code to declare variable in laravel html

@php
$name = "John"
@endphp

You can then use this variable anywhere in your template by referencing it. For example, to print the value of the “name” variable, you would use the following code: {{ $name }} You can also use blade variables in place of HTML attributes. Above we easily declared and change variable value in blade template laravel

What is a Variable?

A variable is a value that can be changed. In programming, variables are used to store data values. In the Laravel Blade template system, variables are used to store data that can be displayed in the template.

Also Read : How to get laravel application root path ?

How to Set a Variable in a Laravel Blade Template

When working with Blade templates, it’s often necessary to declare variables to make information available to the template. There is two ways to Declare variable in laravel blade template:

Method 1: Laravel blade template @php syntax

In this method we will use @php and @endphp tags to set the variable as follow

@php
$name = "John"
@endphp

Method 2: Php tags to declare the variables

In this method we will use <?php and ?> tags to set the variable as follow

<?php 
$name = "John"
?>

Related

Php Laravel Laravel 9 bladedeclarelaravelvariable

Post navigation

Previous post
Next post

Related Posts

Php How to run specific seeder class in laravel

How to run specific seeder class in laravel 9 ?

February 19, 2022February 22, 2022

In our last article we learnt about how to create seeder in laravel but sometimes we only need to run a specific seeders rather then running all so in this article we will cover to run a specific seeder. We will use a simple example of article class and will…

Read More
Php Laravel encrypt and decrypt strings

How to encrypt and decrypt string in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides inbuilt library to encrypt and decrypt the strings. Laravel usage OpenSSL to provide AES-256 encryption which creates long encrypted string. Encryption and decryption is a technique to hide the useful some information in a form which is not readable. In this tutorial i will show you to encrypt…

Read More
Php Ajax laravel Image Upload

Ajax laravel Image Upload with form with example

June 6, 2022November 6, 2023

In this tutorial i will show you to use Ajax laravel image Upload with form in laravel . This can be implement easily using the laravel file and storage providers. in our recent articles of How to Upload image with preview in Laravel 8 with example ? i explained to…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version