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

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.

Related

How to render HTML when inside {{}} on Brade with ternary operator in Laravel 5.7?

I have the follow code in Blade using a ternary operator:
<td>{{isset($arrTemp[$ccc->id]) ? "<a hfet='".url('/cc/'.$cc->id)."'>".count($arrTemp[$cc->id])."</a>": 'N/A'}}</td>
If it find somenthing for the array key $cc->id, should thisplay the value with the link atteched to it.
But the page is rendering <a hfet='http://my.test/cc/56526235'>4</a> the string itself.
What am I missing?
When you use {{ }} the output is automatically escaped to prevent XSS attacks. You can use {!! !!} instead, which will not escape the string.
Source: https://laravel.com/docs/5.4/blade#displaying-data

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.

how to define a variable in laravel blade

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

Laravel 4 Carbon Format with if statement

Does anyone know how I could get this to work.
My database has some null dates, so I would like to return as empty space.
I currently have this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
and from what I read about the blade template is that you can use "or 'message'" after it.
I did this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') or '' }}
in hopes to just show empty space but I get a "1" instead.
Anyone know why and/or how to get around this?
Well, you should rather do it using simple condition:
#if ($chauffeur->roadTestCert !== null)
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
#endif

Ternary in Laravel Blade

Looking for a ternary operator for blade templates
#if(Auth::check()) ? yes : no #endif
Can't seem to get it to work this works
#if(Auth::check()) yes #else no #endif
suppose there is not much in it for this example, just curious.
You are free to use it with {{ }}.
{{ Auth::check() ? 'yes' : 'no' }}
This works:
{{ Auth::check() ? 'yes' : 'no' }}
I know this question was asked a while ago, but this may help somebody.
You can now do this in Laravel 5.
{{ $variable or "default" }}
Laravel 5 Blade Templates
Laravel 5.2 Blade Template
in addition, here is a nice shortcut ?:, if you you need to print some variable's value or if it's empty some default text
{{ $value ?: 'Default Value' }}
For Laravel 5 + php7, you should be using the null coalesce operator as explained in this Laravel News article, like so:
{{ $value ?? "Fallback" }}
Before the null coalescing operator, Blade handled the same problem with the “or” operator, which allows a default value when the first value isn’t set, separated by an “or”.
The ternary conditions look like this:
{{ condition ? 'yes' : 'no' }}
core php
if(!empty($mat->getSupplier)) {
echo $mat->getSupplier->supplier_name;
}
else if(!empty($mat->request_plant) {
echo \App\PlantName::whereId($mat->request_plant)->value('name');
}
else {
echo $mat->other_supplier;
}
laravel blade file
{{ (!empty($mat->getSupplier)) ? $mat->getSupplier->supplier_name : ((!empty($mat->request_plant)) ? \App\PlantName::whereId($mat->request_plant)->value('name'): $mat->other_supplier) }}

Resources