Ternary in Laravel Blade - laravel

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) }}

Related

Ternary Operator in Laravel

I was trying to simply the if operator by using Ternary Operator in Laravel 8 and end up the error shown blow
syntax error, unexpected token "="
Below is the piece of code I have :
{{ $title === "New Member" ? value="{{$data->full_name}}" : value="{{$data->name}}" }}
Please help me to fix that error.
You have to many curly braces in your code, instead try this:
{{ $title === "New Member" ? $data->full_name : $data->name }}
This outputs the full name when the title is New Member, and the name otherwise.
Infact, the double curly braces in Blade are for displaying data, not exactly for assigning variables. For this, you should use the #php directive as so:
#php
$value = ( $title === "New Member" ? $data->full_name : $data->name );
#endphp
// output it
{{ $value }}
Note: The parenthesis are not required, but make the code more readable.

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 5.x and Columns name with blank space

In my blade template I have a problem with a DB column with a blank space.
{{ $data->first name }} doesn't work.
Is there any alternative solution?
P.s. I can't modify my column name.
You can use the following syntax to access the column:
{{ $data->{'first name'} }}
or
{{ $data['first name'] }}

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

Resources