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 }}.
Related
i made a blog in laravel and now created a new migration for a rating system. i call it with
#foreach($blogs as $key => $blog)
$blog->title....
#foreach($ratings as $key => $rating)
#if( $rating->blog_id == $blog->id)
<br> rating found!
<br> {{ $rating->rating_total }}
<br> {{ $rating->rating_amount }}
#endif
#endforeach
#endforeach
it works but it doesnt seem right to run a for loop on every blog post in order to get the rating.
how can i solve this better?
thanks
You could use one that show this. Like this: <input id="input-1" name="input-1" class="rating rating-loading" data-min="0" data-max="5" data-step="0.1" value="{{ $post->averageRating }}" data-size="xs" disabled="">
Since you have the relationship setup from Blog to Rating you can eager load the 'rating' relationship for each Blog post:
$blogs = Blog::with('rating')->...->get();
Then in Blade when you are looping your $blogs you will have access to it's rating:
#foreach($blogs as $blog)
{{ $blog->title }}
#if($blog->rating)
{{ $blog->rating->rating_total }}
{{ $blog->rating->rating_amount }}
#endif
#endforeach
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
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()
?>
I check radio button with value from database, it works, but it has rendered "1" just before that checked radio button. If I remove if statement then it does't show.
<?php $formUserType = UserType::all(); ?>
#foreach($formUserType as $valUT)
{{ $varSetRadio = false }}
#if($user->profile->dic_user_type_id == $valUT->id)
{{ $varSetRadio = true }}
#endif
{!! Form::radio('profile[dic_user_type_id]', $valUT->id, $varSetRadio) !!} {{ $valUT->name }}
<br />
#endforeach
{{ expr }} translates to <?php echo e(expr) ?>.
The output of an assignment is the rightmost value in php, so ($var = 1) is equal to 1 (Also true is equal to 1 because PHP is truthy)
Do the following:
#foreach($formUserType as $value)
{!! Form::radio('profile[dic_user_type_id]', $value->id, $user->profile->dic_user_type_id === $value->id) !!}
{{ $value->name }}
<br />
#endforeach
In addition, you shouldn't be comparing anything but simple logic in a view.
Try adding the comparison for dic_user_type to the User model itself, and pass your data in instead of fetching outside of a controller/model.
I would like to test the existence of value in blade on Laravel 4.
Like:
{{ Form::text('name', #if(isset($value)) {{$value}} #endif; }}
I tried this:
{{ Form::text('name', #if(isset($value)) $value #endif; }}
You can do it in one line.
I'd prefer a better approach, using condition?if:else , which also I use in my daily projects:
{{ Form::text('name',isset($value)?$value:'') }}
or even:
{{ Form::text('name',isset($value)?$value:null) }}
Anywhere before that, you can add the line <?php if(!isset($value)) { $value= ''; } ?>. Then you can proceed as usual {{ Form::text('name', $value) }}