Laravel 4 - Enumerating #foreach - for-loop

I want to enumerate some content in a #foreach. I need something like:
1 - First data
2 - Second data
...
I've displayed the data using a #foreach, but I need to enumerate it. I've tried with #for, combining with #foreach, but it hasn't worked for my... Does anybody know how to do it?
Thank you in advance!!

Assuming the array keys are not the index you want to enumerate, you can do this:
#for ($i = 0; $i < count($data); $i++)
{{ $i }} - {{ $data[$i]->field }}
#endfor
If you can use the array key for enumeration, you can do this:
#foreach ($data as $key => $value)
{{ $key }} - {{ $value->field }}
#endforeach

<?php $i = 1; ?>
#foreach ($collection as $item)
<p>{{ $i }} - {{ $item->property }}</p>
<?php $i++; ?>
#endforeach

Related

How to get the total count of for each in laravel blade

<h3>
#php $i = 0 #endphp
#foreach ($dischargePatients as $patient)
#if ($patient->GetPatientDistrict() == auth()->user()->district_assigned)
{{ ++$i }}
#else
{{ $i }}
#endif
#endforeach
</h3>
the value i get was "0 0 0" , what i need is 3.
there are actually 4 which 3 is true and 1 false
i hope you understand my question, thanks in advance
Its better to count the users in the patientDistrict in the controller and give the view the new variable
$patientsInDistrict = collect($dischargePatients)
->filter(function($patient) {
return $patient->GetPatientDistrict() == auth()->user()->district_assigned;
})
->count()
...
return view('your-view', ['dischargePatients' => $dischargePatients, 'patientsInDistrict' => $patientsInDistrict]);
That gives you a new variable in your view
{{ $patientsInDistrict }}

How to retrieve translation strings inside a php code in laravel blade template

I am trying to use the localization retrieval of string inside of php foreach loop code in blade template in laravel 8.
Inside the foreach loop, I am trying to manipulate a value called $item['label'] and equate translate the value of it using the language localization that laravel have.
Here's my current code.
#foreach ($items as $item)
#php
$item['label'] = "{{ __($item['label']) }}"
#endphp
#endforeach
But I get an error of
ParseError
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
In the first place, can I use a {{ __ ('string') }} or #lang('string') inside a #php in the first place?
If I can't is there any other approach for this one?
Thank you very much!
#php and #endphp is a blade syntax, and it is the same as writing:
<?php ?>
So you could do this:
<?php
echo __('profile/username');
?>
Or you can write it using a Blade template engine:
#php
echo __('profile/username');
#endphp
To print the items you could do this:
#foreach ($items as $key => $item)
{{ __($item) }}
#endforeach
Here an example with data:
#php
$items = ['engine_1' => ['label' => 'Google'], 'engine_2' => ['label' => 'Bing']];
#endphp
#foreach ($items as $key => $item)
{{ __($item['label']) }}
#endforeach
// The output will be => Google Bing
In order to save the translation of the item, remove the "{{ }}" and use the key in order to detect on which index to apply the changes, like the following:
#foreach ($items as $key => $item)
#php
$items[$key]['label'] = __($item['label'])
#endphp
#endforeach
Notice what #Nurbek Boymurodov wrote to you, you need to use the $key, because doing something like this will not override the data within a foreach loop:
#foreach ($items as $key => $item)
#php
$item['label'] = __($item['label']); // Wrong way of overriding data
#endphp
#endforeach
while using foreach you cant change its value here try this this will work if $items is array not stdClass
#foreach ($items as $key => $item)
#php
$items[$key]['label'] = __($item['label']);
#endphp
#endforeach
Thanks, #Nurbek Boymurodov!
It was your comment that answered my question.
Here's the code right now:
#foreach ($items as $item)
#php
$item['label'] = __($item['label']);
#endphp
//other codes where I used the manipulated $item['label']
#endforeach
by just deleting the {{ }} I've manipulated the value that I want, Thank you!

How to display specific data from array?

How can I correct display data from array. I guess it's a foreach loop, but I do not know how to bite it. I just need only header. It's in table calendars.
Thanks so much!
#foreach($list as $item)
{{ $item->header }}
#endforeach
I tried the above code but throws out an error, unknown value "header"
#foreach($results as $type => $list)
<div class="single-contact-information mb-30">
<h3>{{ $type }} </h3>
{{ $list }}
</div>
#endforeach
[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]
Edit:
SearchController:
public function search(Request $request)
{
$search = $request->get('search');
$results = [];
$results['calendars'] = DB::table('calendars')->where('header', 'like', '%'.$search.'%')->get();
return view('pages.search', ['results' => $results]);
}
You should try this:
[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]
Above value is json then use below code
$list = [{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}];
#foreach($list as $item)
{{ $item['header'] }}
#endforeach
$list = json_decode($list);
#foreach($list as $item)
{{ $item['header'] }}
#endforeach
Updated Answer
#foreach(json_decode($list) as $item)
{{ $item->header }}
#endforeach
try this one
used json_decode function here
#foreach(json_decode($list) as $item)
{{ $item['header'] }}
#endforeach

isset($var) - BLADE versus raw php

I am using Laravel 4.2. I have a situation that I need to address in laravel blade..
WITHIN the template I have this ( and it works fine)
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
foreach ($faqGroup as $key => $faq) {
if (isset($faq[$Q]) && isset($faq[$A])) {
?>
<?php echo $faq[$Q]; ?> <br/>
<?php echo $faq[$A]; ?> <br/><br/>
<?php
}
}
?>
my output is
question1 eng
answer1 eng
question2 eng
answer2 eng
question3 eng
answer3 eng
When I try to do the same with blade
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
?>
#foreach ($faqGroup as $key => $faq)
{{ isset($faq[$Q]) }} <br/>
{{ isset($faq[$A]) }} <br/><br/>
#endforeach
my output is
1
1
1
If I try this
#foreach ($faqGroup as $key => $faq)
{{ $faq->$Q }} <br/>
#endforeach
I get Trying to get property of non-object
If I try this
#foreach ($faqGroup as $key => $faq)
{{ $faq[$Q] }} <br/>
#endforeach
I get Illegal string offset 'question_eng'
Help?
Based upon your original code and logic which is working - your #foreach loop should be like this
#forreach ($faqGroup as $key => $faq)
#if (isset($faq[$Q]) && isset($faq[$A]))
{{ $faq[$Q] }} <br/>
{{ $faq[$A] }} <br/><br/>
#endif
#endforeach

increment row number with laravel pagination

How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)
<thead>
<tr>
<th>No</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach ($telephone->results as $telp)
<tr>
<td>
{{$i++}}
</td>
<td>{{ $telp->name }}</td>
</tr>
#endforeach
</tbody>
when i go to page 2 the number will start from 1 again.
i need to make it when i go to page 2 it will start from 4
In Laravel 5.3 you can use firstItem():
#foreach ($items as $key => $value)
{{ $items->firstItem() + $key }}
#endforeach
The below works with laravel 5.4
<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
#foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
#endforeach
Edited
The below works for laravel 5.7 above
#foreach ($telephone as $key=> $whatever)
<td>{{ $key+ $telephone->firstItem() }}</td>
#endforeach
You should be able to use the getFrom method to get the starting number of the current pages results. So instead of setting $i = 1; you should be able to do this.
<?php $i = $telephone->getFrom(); ?>
In Laravel 3 there is no getFrom method so you need to calculate it manually.
<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
Laravel 5.3
#foreach ($products as $key=>$val)
{{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
#endforeach
For Laravel 6.2:
$loop - just in case, is a built-in instance in Blade
#foreach($array as $item)
<tr class="table-row">
<td class="site-id">
{{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
</td>
</tr>
#endforeach
</table>
You can simply add the following line
$i = ($telephone->currentpage()-1)* $telephone->perpage();
in place of
$i = 1;
#php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))
#foreach($yourVariable as $item)
<td>{{ $item->key_name }}</td>
.....
#php($sl++)
#endforeach
In Laravel 6
#php $i = ($data->currentpage()-1)* $data->perpage() + 1;#endphp
#foreach($data as $banner)
<tr>
<td>{{$i}}</td>
<td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
</tr>
#php $i += 1; #endphp
#endforeach
If you are using Laravel Pagination. It works very well
The backend
public function index()
{
return view('your.telephone.index', [
'telephone' => Telephone::paginate(15)
]);
}
Blade front end
<td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration }}</td>
And don't forget embed the page
{{ $telephone->links() }}
For $loop->iteration
See the doc here: https://laravel.com/docs/7.x/blade#the-loop-variable
For $telephone->currentPage()
See the doc here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods
Or avoid php tags completely by
#foreach ($users as $key => $user)
{{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
#endforeach
You can use it in your controller. example given below
$records = Table::paginate(20);
return view('yourview',compact('records')->with('i', ($request->input('page', 1) - 1) * 20);
Note: paginate value must be equal to with() function vlaue
after that you can use $i variable in you blade file. controller calculate value according to page number and return to blade
In blade file. use inside the loop
{{ ++$i }}
Hopefully this will help you
Laravel 8++
$loop->iteration is available out of the box inside loop.
{{ ($barangs->currentPage() - 1) * $barangs->count() + $loop->iteration }}
I'm using this, in Laravel 8, accessing $loop, and links()->paginator:
#foreach ($users as $user)
<tr>
<td class="text-center">{{ ($users->currentPage() - 1) * $users->links()->paginator->perPage() + $loop->iteration }}</td>
<td class="text-center">{{$user->name}}</td>
</tr>
#endforeach
Which works properly even when the final page is only partially filled.
Could also use $users->perPage() in place of $users->links()->paginator->perPage()

Resources