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>
Related
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
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.
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
Using Laravel and Revisionable package. I am populating a table with user record modifications and have the following code snippet:
<tr>
<td width="150">{{ $revision->updated_at }}</td>
<td>User</td>
<td>{{ $revision->revisionable->first_name }} {{ $revision->revisionable->last_name }}</td>
<td width="50">{{ $revision->fieldName() }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td>{{ $revision->oldValue() }}</td>
<td>{{ $revision->newValue() }}</td>
</tr>
Getting the users first and last name works fine when the original user record exists but in some cases the original record (user) is deleted and it causes an error because the user record no longer exists. Is there an IF Statement I can use to first check if the first name field returns an error? Then I can display first/last name for records that still exist and something else if it doesn't exist.
Thanks in advance!
Blade has the orsyntax, really nice:
{{ $revision->revisionable->first_name or 'N/A'}}
{{ $revision->revisionable->last_name or 'N/A'}}
alternatives, though not as elegant:
{{ isset($revision->revisionable->first_name) ? $revision->revisionable->first_name : 'N/A' }}
or
#if (isset($revision->revisionable->first_name))
{{ $revision->revisionable->first_name }}
#else
N/A
#endif
I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
And so on. In my blade template I have the following code:
{{ $i=0 }}
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
#endforeach
Which outputs:
0 First Category
1 Second Category
2 Third Category
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
Thanks.
You just need to use the plain php translation:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
EDIT:
Note the $index will start from 0, So it should be {{ $index+1 }}
The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:
<?php $i=0 ?>
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
<?php $i++ ?>
#endforeach
As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.
#foreach($cats as $cat)
{{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
#endforeach
<? php $i = 0 ?>
#foreach ( $variable_name as $value )
{{ $ value }}<br />
< ? php $i++ ?>
#endforeach
if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after
#foreach ($posts as $k => $post)
{{ $loop->iteration }}. {{ $post->name }}
#endforeach
You can actually use a built in helper for this: {{ $cat->incrementing }}.