checking variable is null in laravel blade file - laravel

I have the variable $material_details->pricing=null I want to check the variable is set in laravel blade file.I have tried as
#isset($material_details->pricing)
<tr>
<td>price is not null</td>
</tr>
#endisset
but no luck .How to check the variable is set or not in laravel 5.3 blade file.

You can use either Null Coalescing or Elvis Operator
Null Coalescing Operator
{{ $material_details ?? 'second value' }} //object
{{ $material_details->property ?? 'second value' }} //Object Property
Elvis Operator (checks given value is empty or null)
{{ $material_details ?: 'default value' }} //Object
{{ $material_details->property ?: 'default value' }} //Object Property
Read on Elvis and Null coalescing operator.
Links:
Elvis: https://en.wikipedia.org/wiki/Elvis_operator
Null coalescing operator: https://en.wikipedia.org/wiki/Null_coalescing_operator

Try following code.
#if(is_null($material_details))
// whatever you need to do here
#else

Try this
#if(isset($material_details->pricing))
<tr>
<td>NOT NULL</td>
</tr>
#else
<tr>
<td>NULL</td>
</tr>
#endif
Or this
#if(empty($material_details->pricing))
<tr>
<td>NULL</td>
</tr>
#else
<tr>
<td>NOT NULL</td>
</tr>
#endif

Your code will only print data if the variable hold, some value.
Try something like following :
#if(isset($material_details->pricing))
<tr>
<td>price is not null</td>
</tr>
#else
<tr>
<td>null</td>
</tr>
#endif

You can do it by using laravel ternary operator as like below
{!! !empty($material_details->pricing) ? '<tr><td>price is not null</td></tr>' : '<tr><td>Empty</td</tr>' !!}

You can also use '#isset()' balde directive :
#isset($records)
// $records is defined and is not null...
#endisset
#empty($records)
// $records is "empty"...
#endempty

Please try this
#if($material_details->pricing != null)
<tr>
<td>price is not null</td>
</tr>
#endif

If are able to access your variable through {{ $material_details['pricing'] }} inside your blade file,
then this will work to check and append value:
{{ ($material_details['pricing'] == "null") ? "null value" : $material_details['pricing'] }}
To check and append elements :
#if($material_details['pricing'] != "null")
<tr>
<td>price is not null</td>
</tr>
#endif

You can simply use Laravel isEmpty() by chaining it to your collection on blade.
#if($data->quotes->isEmpty())
//Empty
#else
// Not Empty
#endif
More info on Laravel API website here.

Related

How to write if else statement in laravel 8

See I can write some code in PHP I want to write the same code in laravel-8 how can I?
My PHP code
<td>
<?PHP
if($Runs>0 and $Balls==0){
echo $Runs*100;
}elseif($Balls>0 and $Runs==0){
echo $Balls*$Runs;
}elseif($Balls==0 and $Runs==0){
echo $Balls*$Runs;
}
elseif($Runs>0 and $Balls>=0){
echo $Runs/$Balls*100;
}
?>
</td>
I want to write this same code in laravel-8
This is what I can do
<td>
{{ $value->runs/$value->balls*100 }}
</td>
I can't write if else condition there How can I?
You can use as like below.
#if($Runs>0 and $Balls==0)
{{ $Runs*100 }}
#elseif ($Balls>0 and $Runs==0)
{{ $Balls*$Runs }}
#else
your else code
#endif
You can check all blade template condition in document to

Add multiple classes to element with #if statement

I have a following problem. I am checking if arrays are empty and if they are i am hiding specific elements in table. This implementation adds only 1 class but is there a way to add multiple without using javascript?
<table #if(empty($a)) class="hide_a"#endif
#if(empty($aa)) class="hide_aa"#endif
#if(empty($aaa)) class="hide_aaa"#endif>
The Problem is, that a html element can only have one class attribute.
Simply check the value inside the class attribute
<table class="{{ empty($a) ? 'hide_a' : '' }} {{ empty($aa) ? 'hide_aa' : '' }} {{ empty($aaa) ? 'hide_aaa' : '' }}">
Yes, you can do it as below.
<table class="#if(empty($a)) hide_a #endif
#if(empty($aa)) hide_aa #endif
#if(empty($aaa)) hide_aaa #endif">
<table class="#if(empty($a)) hide_a #endif
#if(empty($aa)) hide_aa #endif
#if(empty($aaa)) hide_aaa #endif">

Level 7 display relationship data in blade using one to many

whats wrong why in blade don't want to show the LEVEL name
controller:
$streams=Stream::with('level')->get();
return view('streams.index',compact('streams'));
Stream Model:
public function level()
{
return $this->belongsTo('App\Models\Level', 'level_id');
}
Level Model:
public function stream()
{
return $this->hasMany('App\Models\Stream', 'level_id');
}
Blade Index:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
<td>
{{$stream->level->name}}
</td>
<td>
<tr/>
#endforeach
Also tried:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
#foreach($stream as $level)
<td>
{{$level->name}}
</td>
#endforeach
<td>
<tr/>
#endforeach
Error:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\project\sms\resources\views\streams\index.blade.php)
DD result:
[{"id":3,"name":"Class A","code":"GRC0001A","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","level_id":1,"created_at":"2020-06-14T10:58:28.000000Z","updated_at":"2020-06-14T11:39:54.000000Z","level":{"id":1,"name":"YEAR 7","code":"CODE1","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","created_at":"-000001-11-30T00:00:00.000000Z","updated_at":"-000001-11-30T00:00:00.000000Z"}},{"id":4,"name":"Class B","code":"GRC0001A","uuid":"gq2kZZikN76XEa4pQWsyAZBMxjKeHBJt0a840ZMSiuHGztuhYT0G6q5WcGgp8z6BD6nx0WSrrOTvEb4iQ0ewyB9Fa1M54CAv8HS2","level_id":null,"created_at":"2020-06-14T10:58:36.000000Z","updated_at":"2020-06-14T11:39:59.000000Z","level":null}]
Your first Blade template is almost correct.
The issue is that one or more of your records has no Level assigned (level_id=null), but you're still trying to pull the name property from $stream->level, which is null.
Simply add a check for $stream->level before trying to access/print its properties. For example:
#if($stream->level)
{{ $stream->level->name }}
#endif
or
{{ $stream->level ? $stream->level->name : 'No Level Attached' }}
or
{{ optional($stream->level)->name }}
etc.

Data printed twice in first iteration

I'm using laravel blade. I try to print array of data as a column in a table. then, I put a conditional expression inside the loop to avoid printing the same value in the same column. After that my data printed twice in the first iteration.Here is the code
{{ $prevKomponen = null }}
{{ $prevKegiatan = null }}
#foreach($data as $key => $value)
<tr>
#if ($prevKomponen != $value['komponen'])
<td>{{ $value['komponen'] }}***</td>
#else
<td>-------</td>
#endif
{{ $prevKomponen = $value['komponen'] }}
<td>{{$value['kegiatan']}}</td>
<td>{{$value['subkegiatan']}}</td>
<td>{{$value['rincian']}}</td>
<td>{{$value['jumlah']}}</td>
<td>{{$value['harga']}}</td>
<td>{{$value['total_harga']}}</td>
</tr>
#endforeach
And here is the result. Data in column 1 is also printed in column 2
Any help to address this problem ?
This must be caused by https://laravel.com/docs/5.5/blade#displaying-data. It says
"You may display data passed to your Blade views by wrapping the variable in curly braces".
So, when you hit these line:
{{ $prevKomponen = $value['komponen'] }}
It will display as output.
But, if you want to ignore it, you can add '#' symbol, just like this:
#{{ $prevKomponen = $value['komponen'] }}
'#' symbol to inform the Blade rendering engine an expression should remain untouched. Hopefully this will help you.
you have missing a td, try this snipet in your if condition
#if ($prevKomponen != $value['komponen'])
<td>{{ $value['komponen'] }}***</td>
#else
<td>-------</td>
#endif
<td>{{ $prevKomponen = $value['komponen'] }}</td>

Laravel blade compare two date

I would like to compare 2 dates. So I create a condition like this in my template blade:
#if(\Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') < $dateNow)
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#endif
My variable $dateNow has the same format on my value in $contract->date_facturation
Screenshot
It add a red background on the date 25/02/2018 while the date is not less than the variable $contract->date_facturation
Do you have any idea of ​​the problem?
Thank you
The problem is that you are trying to compare two date strings. PHP don't know how to compare date strings.
Carbon::format() returns a string. You shoud convert your dates to a Carbon object (using parse) and use Carbon's comparison methods, as described on Carbon Docs (Comparison).
For your example, you should do:
// Note that for this work, $dateNow MUST be a carbon instance.
#if(\Carbon\Carbon::parse($contrat->date_facturation)->lt($dateNow))
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
#endif
Also your code looks repetitive, and assuming that $dateNow is a variable with current date, you can use Carbon::isPast() method, so rewriting your code, it becomes:
#php($date_facturation = \Carbon\Carbon::parse($contrat->date_facturation))
#if ($date_facturation->isPast())
<td class="danger">
#else
<td>
#endif
{{ $date_facturation->format('d/m/Y') }}
</td>
This makes your code less repetitive and faster, since you parse the date once, instead of twice.
Also if you want your code better, use Eloquent's Date Mutators, so you won't need to parse dates everytime that you need on your views.
You can parse both dates, change your code to:
<td class="{{ Carbon\Carbon::parse($contrat->date_facturation) < Carbon\Carbon::parse($dateNow) ? 'danger' : '' }}">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
Alternatively, you can use the date mutators. In this case you'll not need to parse dates.
Compare date in blade template
I used this method of Carbon:
#foreach($earnings as $data)
#if($data->created_at == Carbon\Carbon::today())
<tr class="alert alert-success">
#else
<tr>
#endif
....
#endforeach
More Details

Resources