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 Change Date Format in Blade View File in Laravel

How to Change Date Format in Blade View File in Laravel ?

September 8, 2022March 16, 2024

Laravel default shows the date in the way its stored in database bu sometimes we want to change date format in blade view file in laravel according to our requirements. It can be multiple possibilities and requirement from client or product manager to change the date format so in this…

Read More
Php Laravel slug generator using trait

Laravel reusable slug generator using trait

November 8, 2021November 8, 2021

In our last article Laravel slug generator with example we demonstrate how we can generate slug for model but in that article we need to rewrite the same code for multiple models if we want use the slug generation in multiple model. so in this article i will show you…

Read More
Laravel 3 Ways to Remove public from URL in Laravel

3 Ways to Remove public from URL in Laravel

August 22, 2021November 8, 2023

Laravel comes with default server for local environment and but if run the application in apache then we need to remove the public from URL because if we serve the application through the Apache then we need to call the URLs from public directory. It can be multiple way 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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version