need to get next Task after the one in the URL - laravel

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

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

How to display default image if no filename exists in Laravel database

I want to display an image for each entry in a list of posts. The name of the images are unique file names. They are all saved in a database (MySQL) table. There is more than one image for each post. The code works right. Except, when there is a post without an image filename. This is a possible scenario but i can't get the code to work. In the event there's no existing filename for a post, i want to display a default filename.
Here's my code:
Images logic:
/**
* Get a vehicle's top or main image data from the image table
*/
public function topImage($id)
{
$image = Vehiclefulldataimage::where('vehiclefulldatas_id', $id)
->orderBy('id', 'ASC')->first();
return $image;
}
Here's the blade view, Posts:
#if (count($posts) > 0)
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
#if ($imageFilename = 0)
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif
This is the error message i get:
"Trying to get property of Non Object"
Try the following code:
You are using an assignment operator instead of comparison operator
//#if (count($posts) > 0)
#if (!$posts->isEmpty())
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
//#if ($imageFilename = 0) this is an assignment operator not comparison operator
#if ($imageFilename->isEmpty())
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif
Check your if statement....what you're doing there is an assignment and not a condition test...you can include a column..with a string pointing to the path of your default image..and set it to default...then load it...as default image
All of the answers above fix your problem. However, I recommend you to fix this problem by using Laravel features, Mutators.
https://laravel.com/docs/5.8/eloquent-mutators
Laravel mutators allow you to format or modify your Model attribute.
Just in your Vehiclefulldataimage model write the code below:
public function getImageAttribute($value)
{
//can write more logic here.
return $value ?: 'path/to/your/default/image';
}
Then you don't care more if condition blade template. If your image is empty the mutator returns the default image. So, everywhere when your retrieve Vehiclefulldataimage instance the mutator works properly
Thanks everyone.
After looking at all the answers, i did a simple alteration to my code. I altered:
#if ($imageFilename = 0)
to:
#if ($imageFilename == null)
Thus, my code now looks:
#if (count($posts) > 0)
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
#if ($imageFilename == null)
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif

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

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

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