how to define a variable in laravel blade - laravel-5

How can define a variable in the view side(blade) in laravel?
I found that I can do it in this way:
<?php $var = 'something' ?>
But is there any way to do this like {{ $var = 'something' }} or #var1 = 'something' ?(ofcourse without printing it)

I agree with #Kiril Ivanov answer, but if you still want to do that you can use
#php ($variable = 'test')
Thanks

no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements

yes there is a way to do this
first assign your variable like this
{{ $yourvariable='' }}
and after than u can manipulate the variable
#if ($abc['type']=='youresult')
{{ $yourvariable='success'}}
#endif
Hpe this works

Related

How to directly call the laravel sanitize function?

In a blade file one can do this:
{{ $someVariable }}
This sanitizes $someVariable as opposed to calling it like this:
{!! $someVariable !!}
What PHP function is called for the first case? Is there a way to do this outside of a blade file?
The function that ends up being called is e, for 'escape'.
"Encode HTML special characters in a string."
{{ ... }} is replaced with <?php echo e(...); ?>.
It is defined in vendor/laravel/framework/src/Illuminate/Support/helpers.php. It calls htmlspecialchars but also handles special objects that are Htmlable or DeferringDisplayableValue.
"The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default" - Laravel 9.x Docs - Helpers - String Helpers - e
On a side note, this is not sanitizing, it is just escaping.
According to the Laravel documentation you can do it with htmlspecialchars()
Example:
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
https://www.php.net/manual/en/function.htmlspecialchars.php

Can I use a tenary operator when passing blade attribute to child component

pretty straigth forward. I would like to know if I can use tenary operators or null coalesce operators when I pass an attribute to a child component in blade.
For example
<x-filter :categories="{{ isset($categories) ? $categories : false }}" />
or
<x-filter :categories="{{ $categories ?? false }}" />
I have not been successful with this in laravel 8.
Am I having an error in syntax or is this just not possible?
Best Simon
TL;DR:
Use {{ }} OR : but not both of them
When you place a : before the attribute this means you are writing php within your double quotes, so as #lagbox mentioned in his comment just remove it. OR remove the colon and you are good to go, nothing fancy here.

Laravel blade template once echoing variable and once not error

I recently found strange behavior of blade in Laravel 5.8.
Let's say I have in file
#extends('layout')
#section('title','Dodaj playlistę')
#section('content')
{{$hosts = \App\Host::all()}}
<h2>Dodaj audycję do bazy</h2>
in the middle of the file I have
{{$hosts = \App\Host::all()}}
#foreach ($hosts as $man)
<option value='{{$man->id}}'>{{$man->name}} </option>
#endforeach
The problem is I always get the first $hosts variable echoed, while second not. What the hell? Such variable shouldn't be echoed at all because the command is only variable value attribution.
I've checked all my routes, there is no dd() or var_dump() command anyhere.
Looks like possibly a typo pushing for two sections within one. It should normally throw an error, but might be getting confused with the different language.
Change #section('title','Dodaj playlistę') to #section('title') and give it a try.
Also, set the var directly in the foreach for better clarity:
#foreach ($hosts as $man)
Becomes
#foreach(\App\Host::all() as $man)
{{$hosts = \App\Host::all()}} that will echo "\App\Host::all()"
you can
#php
$hosts = \App\Host::all()
#endphp
but it is discouraged to do in a view
see here How to Set Variables in a Laravel Blade Template

Laravel blade syntax for #if/#endif spacing issueif

what is the similar blade syntax for the following code?
<?php if(...):?>abc<?php else:?>cde<?php endif;?>
the code
#if(...) abc #else .... is no good as it ads a space before and after the "abc" the code #if(...)abc#endif (no spacing before and after html string) generates error ...
Thanks
Solution
The correct solution for this problem would be following:
#if(1==1){{ '1' }}#endif
This happens often and makes problem with "space sensitive" parts of code like values in <option> tags.
Having the #if(1==1) 1 #endif would be compiled to following which shows empty spaces around the number:
<?php if(1==1): ?> 1 <?php endif; ?>
Solution code would be compiled to this code:
<?php if(1==1): ?><?php echo e('1'); ?><?php endif; ?>
Which pretty much explains why this won't make additional spaces.
Did a bit more research and it looks like there is no solution as there is no spaceless tag in blade. I found a solution from someone who wrapping his string in extra hml tags (so that is easy as he ads spaces before and after the tag and the string inside tag si spaceless) but in my case I just need a string no spaces before and after ... I will go the old fashion php way ...
Try this:
#if(...) {{ 'abc' }} #else
I've run into similar problems with spaces.
A workaround I use is this:
Put your if statement into php tags
#php
$myVar = 'abc';
if(...) $myVar = 'cde';
#endphp
and then echo the defined variable
{{$myVar}}
It's a workaround, but I still think we always should remember that it's a PHP environment...
So, simply:
#php
if(...) echo "abc";
else echo "cde";
#endphp
I have found the solution in case there is space issue between #if()#endif and #if.
I have replaced the #if() #endif with the {{$wardname}} variable to be printed using {{$wardname}}#if and its removed conflict with #endif & #if
and applied logic like:
if($city->wardname!="") {
$wardname = "(".$city->wardname.")";
}else{
$wardname = "";
}
Implemented it as:
{{$wardname}}#if
The correct syntax is:
#if(...)
abc
#else
cde
#endif
I never saw unwanted spaces with this one.

if the array were empty in laravel . then how to dispaly the message that their are no record found

#foreach($array as $value)
{{$value}}
#endforeach
$array is my array pass from Laravel controller then i want to display the message record not found if the array were empty.
this is my code example.
Besides 'foreach' loops, we also got 'forelse' loops in laravel blade template, what exactly this 'forelse' loops does? and most importantly should we care about it?
The 'forelse' loops is the better version of 'foreach', so yes, you should care, 'forelse' loops works exactly as 'foreach' except it also check the value is empty or not.
So with 'foreach' normally you check first whether the value is empty or not using 'if' statement, using 'forelse' you don't need to do that, the value is automatically checked.
#forelse ($array as $value)
{{ $value }}
#empty
There are no record found.
#endforelse
You can use #forelseas Shoukat Mirza in his answer and you also could use #if clause:
#if (count($array) > 0)
// foreach loop here
#endif
This is also helpful when you have multiple conditionals to check.

Resources