Laravel if else statement - laravel

I´m trying to output 2 different things. For example: if title is greater then 0 then do this. If not, do this.
I'm using DomDocument & Laravel 5.4
In my controller:
$title = $dom->getElementsByTagName('title');
To output on the page:
#foreach ($title as $node)
#if(!$node > 0)
{{'No title'}}
#else
{{$node->nodeValue, PHP_EOL}} <br />
#endif
#endforeach
The problem: If there is a title it displays the title. If there is no title it shows nothing. I want to display: "No title".
Why isn't this working?

You should do this:
#if (condition)
No title
#else
But I doubt !$node > 0 part does what you want.
It's better to use ternary operator:
#foreach ($title as $node)
{{ empty($node->nodeValue) ? '' : $node->nodeValue }} <br />
#endforeach

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

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()
?>

Laravel Blade - check if data array has specific key

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

How to get all records for current year?

How to get all articles for current year?
Articles::all()->where('created_at',$current_year);
You can do this:
Articles::whereBetween('created_at', [
Carbon::now()->startOfYear(),
Carbon::now()->endOfYear(),
]);
Another way is to use whereYear() as in #xhulio answer. But I'd recommend you to use this code instead as it's more readable:
Articles::whereYear('created_at', date('Y'))->get();
Try the following query
Articles::whereYear('created_at', '=', Carbon::parse($date)->format('Y'))->get();
Laravel has the helper functions whereDate, whereDay, whereMonth and whereYear to better deal with datetime queries.
Get Data via carbon in single line
Articles::whereYear('created_at', Carbon::now()->year)->get();
i found this way to collect all years only is good
#if(isset($years) && $years != '')
#foreach($years as $yrs)
#php($year[] = $yrs->created_at)
#endforeach
#if(isset($year))
#php( $collection = collect($year)->unique()->values()->all())
#foreach($collection as $created)
#php($year_only[] = $created->format('Y'))
#endforeach
#php( $collections = collect($year_only)->unique()->values()->all())
#foreach($collections as $created_year)
#if($created != '')
<div class="lab-item">
<div class="lab-inner" style="border: #ee3725 2px solid;border-radius: 25px;background: bisque;">
<div class="lab-content">
<h4> {{ $created_year }} </h4>
</div>
</div>
</div>
#endif
#endforeach
#endif
#endif

Ternary in Laravel Blade to apply row class names

I have this report that Im trying to do, but I want to make the rows alternate colors. this is what I tried, but it does not work. What is the correct way to achieve this?
<div class="row">
{{$rowOrder = "even"}}
#foreach($data as $row)
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->adm_referraldate}}</span>
<span>{{$row->adm_number}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
#endforeach
</div>
Replace
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
with
<?php $rowOrder = ($rowOrder == "odd") ? 'even' : 'odd'; ?>
or if you are using a Laravel 5.2 or up
#php($rowOrder = ($rowOrder == "odd") ? 'even' : 'odd')
Do the same for the line {{$rowOrder = "even"}}
If you used the {{$rowOrder = "even"}} it will echo out the result.
You can use modulo arithmetic to decide whether and index is odd or even:
$isEven = index % 2
If you combine this with a PHP ternary operator then you'd get this
{{ $loop->index % 2 ? 'odd': 'even' }}
see
https://davidwalsh.name/php-shorthand-if-else-ternary-operators
and
https://en.wikipedia.org/wiki/Modular_arithmetic
Here's a very easy solution:
#php $count = 0; #endphp
#foreach($data as $row)
<div class="{{ ++$count % 2 ? 'odd': 'even' }}">
{{ $row->name }}
</div>
#endforeach
Use variable $loop documentation ( $loop->even laravel 5.8, or ($loop->iteration % 2)laravel< 5.8 )
#foreach ($users as $user)
#if ($loop->even)
This is even.
#else
#endif
#endforeach
or
#foreach ($listObject as $Object)
<tr class="{{ ($loop->iteration % 2) ? 'odd' : 'even' }}">
#endforeach
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
possibly should be
{{ $rowLine = ($rowOrder == "odd" ? 'even' : 'odd') }}
Here's a working example for me: I left the dump output in it so you can see the actual number counting up. Hope it helps anyone who comes across this problem :). EDIT: Don't forget to add colors in your css file for .odd and .even!
#if(!empty($names))
{{-- SET VARIABLE + HIDE IT --}}
<div class="hide">{!! $number = 0 !!}</div>
#foreach($names as $n)
{{ dump($number) }}
<div class="{!! $number % 2 == 0 ? 'odd' : 'even' !!}">
{{-- UP VARIABLE + HIDE IT --}}
<div class="hide">{!! $number++ !!}}</div>
{{-- DISPLAY CONTENT —}}
{{ $n }}
</div>
#endforeach
#endif
Try getting the key from the foreach loop and run ($key % 2)
Basically Odd Number mod 2 always have a remainder
#foreach ($rows as $key => $row)
<div class="#if ($key > 0 && $key % 2) odd #else even #endif">
</div>
#endforeach

Resources