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">
Related
In my Laravel-5.8 blade that has this code:
#foreach($employees as $key => $employee)
<li class="list-group-item">
<b>Line Manager:</b>
#if(!$employee->linemanager)
<a class="float-right">N/A</a>
#else
<a class="float-right">{{ $employee->linemanager->fullName() ?? 'None' }}</a>
#endif
</li>
<li class="list-group-item">
<b>HOD:</b>
#if(!$employee->department->depthead)
<a class="float-right">N/A</a>
#else
<a class="float-right">{{isset($employee->department->depthead) ? $employee->department->depthead->first_name. ' ' .$employee->department->depthead->last_name : 'None'}}</a>
#endif
</li>
#endforeach
and its pointing to this line:
#if(!$employee->department->depthead)
How do I resolve it?
Thanks
The root cause of your issue is that $employee->department is either null or not an Object (Instance of Department, assuming you have Model and Relationships setup properly). You've got a check in place, but it's in the wrong spot. Check for $employee->department and $employee->department->depthead:
#if($employee->department)
#if($employee->department->depthead)
{{ $employee->department->depthead->first_name. ' ' .$employee->department->depthead->last_name }}
#else
None
#endif
#else
N/A
#endif
The defaults returned from a properly formed relationship will remove the need for string checks, meaning that #if($employee->department) and #if($employee->department->depthead) will be "truthy" or "falsey" enough to handle your use case.
Sidenote, this complexity is best moved to a Model function. In your Employee model, add this function:
public function getDepartmentHeadAttribute(){
if ($this->department) {
if ($this->department->depthead) {
return "{$this->department->depthead->first_name} {$this->department->depthead->last_name}";
} else {
return 'None';
}
} else {
return 'N/A';
}
}
Then, in your .blade.php file, you can simply do:
#foreach($employees as $employee)
{{ $employee->department_head }}
#endforeach
And it will perform the required if checks inside your Model and output First/Last, None or N/A as requird.
#if(!$employee->department->depthead)
look like the employee->department is not null, but it is not an object too ...
may be it is a string or number ....
#if(!$employee->department->depthead)
you could use 'is_object' method to determine that ...
#if( is_object($employee->department))
#if(!$employee->department->depthead)
....
#endIf
#endIf
but i don't think that the problem ...
the problem maybe you miss loading the relation,
if the property 'department' is a field of type 'json'
don't forget to cast it in model to 'array'
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 need to check if the data array has a specific key, I tried it like this:
#if ( ! empty($data['currentOffset']) )
<p>Current Offset: {{ $currentOffset }} </p>
#else
<p>The key `currentOffset` is not in the data array</p>
#endif
But I always get <p>The keycurrentOffsetis not in the data array</p>.
You can use #isset:
#isset($data['currentOffset'])
{{-- currentOffset exists --}}
#endisset
I think you need something like this:
#if ( isset($data[$currentOffset]) )
...
Use following:
#if (array_key_exists('currentOffset', $data))
<p>Current Offset: {{ $data['currentOffset'] }} </p>
#else
<p>The key `currentOffset` is not in the data array</p>
#endif
ternary
#php ($currentOffset = isset($data['currentOffset']) ? $data['currentOffset'] : '')
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
I am new to Laravel and don't know if it is possible at all to concatenate a string to a blade variable.
I want to display the name of the author with the ~ sign concatenated to the author's name from the left. Here is my code.
<div class="author">~{{ $quote->author or '' }}</div>
What I want is if the author is set, it should be displayed with the ~.
How do I concatenate this?
Thanks for any help
This would be better
<div class="author">~{{ isset($quote->author) ? $quote->author : '' }}</div>
UPDATE:
<div class="author">{{ isset($quote->author) ? '~'.$quote->author : '' }}</div>
<div class="author">
#if (isset($quote->author))
~ {{ $quote->author }}
#else
#endif
</div>
https://laravel.com/docs/5.3/blade#control-structures