Laravel Blade - check if data array has specific key - laravel

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'] : '')

Related

Foreign key is now null and when mapping through the data it say "Attempt to read property "$category_name" on null"

I deleted a category which is an f-key of the items table. Now the view has an error saying 'Attempt to read property "category_name" on null'. How do I render a null foreign key?
I tried if-else:
#if({{$item->category->category_name}} === 'null')
<h2>no category</h2>
#else
{{ $item->category->category_name}}
#endif
Try this:
#if ($item->category)
{{ $item->category->category_name }}
#else
<h2>no category</h2>
#endif
Or simply
<h2>{{ $item->category?->category_name ?? 'no category' }}</h2>
In PHP8 or Larvel 8
simple use this one
<h2>{{ $item->category->category_name ? 'no category' }}</h2>

Laravel - ERROR: Trying to get property 'depthead' of non-object

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'

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">

need to get next Task after the one in the URL

this for each gave me task where task slug is the same as URL now I need to get the first task after this one so I make redirect link later
#foreach ($Tasks as $Task)
#if ( $Task->slug == Request::segment(5) )
<h2> {{ $Task->task_name }} </h2>
#endif
#endforeach
I need to get the task how is after the one in URL from foreach
Try this code. I hope this will help you
#php
$i = 0
#endphp
#foreach ($Tasks as $Task)
#if ($i > 0)
// write your first task logic here
#endif
#if ( $Task->slug == Request::segment(5) )
<h2> {{ $Task->task_name }} </h2>
#php
$i = $i + 1
#endphp
#endif
#endforeach
Would Laravel's pagination give you the features you need more easily?
In your controller:
$tasks = Task::where('slug', request()->segment(5))->paginate();
You can the use the following in your blade:
<?php
$tasks->nextPageUrl()
?>

How do we concatenate using blade template engine Laravel

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

Resources