How to write if else statement in laravel 8 - laravel

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

Related

How to customize the emails sent by Laravel Breeze?

I wanted to customize the emails sent by Breeze from the default styles. Looks like the installation of Breeze does not publish the files for that but I found online that using php artisan vendor:publish --tag=laravel-notifications will publish the email files, and it did, although looks like it did not publish all the necessary files (it only published one file)
It only published /vendor/notifications/email.blade.php, with the following code, but looks like other things are missing like the components themselves such as the x-mail::button?
<x-mail::message>
{{-- Greeting --}}
#if (! empty($greeting))
# {{ $greeting }}
#else
#if ($level === 'error')
# #lang('Whoops!')
#else
# #lang('Hello!')
#endif
#endif
{{-- Intro Lines --}}
#foreach ($introLines as $line)
{{ $line }}
#endforeach
{{-- Action Button --}}
#isset($actionText)
<?php
$color = match ($level) {
'success', 'error' => $level,
default => 'primary',
};
?>
<x-mail::button :url="$actionUrl" :color="$color">
{{ $actionText }}
</x-mail::button>
#endisset
{{-- Outro Lines --}}
#foreach ($outroLines as $line)
{{ $line }}
#endforeach
{{-- Salutation --}}
#if (! empty($salutation))
{{ $salutation }}
#else
#lang('Regards'),<br>
{{ config('app.name') }}
#endif
{{-- Subcopy --}}
#isset($actionText)
<x-slot:subcopy>
#lang(
"If you're having trouble clicking the \":actionText\" button, copy and paste the URL below\n".
'into your web browser:',
[
'actionText' => $actionText,
]
) <span class="break-all">[{{ $displayableActionUrl }}]({{ $actionUrl }})</span>
</x-slot:subcopy>
#endisset
</x-mail::message>
How can I customize that? The buttons for example? Because looks like this template is working for all the types of emails (welcome, verification, password-reset), so I can't just replace the contents with my code but I somehow need only to change the styles

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 if else statement

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

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

Laravel 4 adding numbers from foreach loop when count variable is supplied?

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 }}.

Resources