How to put spaces in blade template - php

I'm working with https://github.com/cntaoyu/CI-Blade framework and my code is : (works fine and prints too)
#foreach($tbl_data as $d)
#foreach($d as $c)
{{$c}} //I want space after this, new line in fact
#endforeach
#endforeach

#foreach($tbl_data as $d)
#foreach($d as $c)
{{$c}} <br> //if it is blade
#endforeach
#endforeach

Related

Getting weird undefined foreach error in Laravel Blade

I'm working with Laravel 5.8 and I have this syntax:
#php($counter_foreach = 0)
#foreach($memnames as $mem=>$memname)
#php
#endphp
#endforeach
Now when I run this, I get syntax error, unexpected 'endforeach' (T_ENDFOREACH) error:
But when I remove #php #endphp, and replace it with html, the error will be gone!
So what's going wrong here?
Is there anything wrong about using #php after #foreach in Laravel Blades?
Try to add #endphp everytime you add #php like this:
#php($counter_foreach = 0)
#endphp
#foreach($memnames as $mem=>$memname)
#php
#endphp
#endforeach
You actually do not need to make separate php variable for the iterations, so since you have a foreach loop already your logic should be something like this.
#foreach($this as $that) {
#if ($loop->first)
// some logic
#endif
#if ($loop->last)
// some logic
#endif
#endforeach
The $loop variable is available inside the foreach and you check for the iteration number you need inside, I just added some basic uses. Here is some more useful documentation.

Prevent foreach from loop html tag multiple time in blade laravel

I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach

print comma separated value from database

I have a table column which is separate with comma. Column name is 'Intro' and the values are:
Rumi, school,2018,lecturer
I need to show this comma separated value one by one. The output should be like this :
Rumi
I tried this code in my blade file :
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro[0]}}
#endforeach
#endforeach
but this shows nothing. what is the problem ?
Just simply remove the index
{{ $client_intro[0]}} ---->> should be --> {{ $client_intro}}
Try This:
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro }}
#endforeach
#endforeach

Laravel - Foreach loop - Show a specific message for types of result

So I have a messages table with all messages. I want pinned messages to be at top and normal ones to follow after. I have been able to do this normally with the orderBy() method in my query.
However, I want to display some specific messages for all my pinned messages. I would like to have a header at the top of the pinned messages and a header at the top of the normal messages to let users know that pinned and normal messages are there.
Example:
My query:
$rows = Messages::where('active', true)->orderBy('pinned', 'desc')->get();
My view
#foreach ($rows as $row)
{{ $row->message }}
#endforeach
What I see
Message text 3
Message text 1
Message text 2
I have a few messages with "pinned" in the column in database. So I want the pinned ones to show at the top WITH DESCRIPTIONS. Something like this:
Pinned
----------
Message text 3
----------
Normal
----------
Message text 1
Message text 2
I have tried orderBy() and it's working pretty good, in terms of ordering it from pinned to normal, but I can't get it to show the "Pinned" and "Normal" message. How can I do this?
Try something like this (change 1/0 to true/false or whatever you use):
In a controller:
$pinned = $rows->where('pinned', 1);
$normal = $rows->where('pinned', 0);
In a view:
#if(count($pinned) > 0)
Pinned
#foreach ($pinned as $row)
{{ $row->message }}
#endforeach
#endif
#if(count($normal) > 0)
Normal
#foreach ($normal as $row)
{{ $row->message }}
#endforeach
#endif
If real #foreach part is big, use partial and #each instead of #foreach to avoid code duplication.
Alternative
#foreach ($rows as $row)
#if ($row->pinned === 1 && !isset($pinnedShown))
Pinned
{{ $pinnedShown = true }}
#endif
#if ($row->pinned === 0 && !isset($normalShown))
Normal
{{ $normalShown = true }}
#endif
{{ $row->message }}
#endforeach
Short alternative
Not very readable, but if you just need short code, use something like this:
#foreach ($rows as $row)
<?php !($row->pinned == 1 && !isset($pin)) ? : $pin = call_user_func(function(){ echo 'Pinned'; return 1; });
!($row->pinned == 0 && !isset($nor)) ? : $nor = call_user_func(function(){ echo 'Normal'; return 1; }); ?>
{{ $row->message }}
#endforeach

Laravel 5.1 pagination count

I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.
I've found some threads on stackoverflow solving this issue for Laravel 4
Where they say, you should solve it like this
<?php $count = $players->getFrom(); ?>
#foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
#endforeach
or like this
<?php $count = $players->getFrom() + 1; ?>
#foreach ($players as $player)
...
The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.
I tried it like this
<?php $count = $pages->firstItem() + 1 ?>
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
and like this
{{ $count = $pages->firstItem() }}
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
//pages.singlebasic.blade.php
{{ $count ++ }}
but my site always displays only the number "2" instead of counting up. How do I achieve that?
Just use this to get pages count
$players->lastPage()
or this to get items count
$players->total()
If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that
<?php $count++; ?>
{{ $count }}
First of all get 100 players with paginate(100) method then you can get the index or count in the following way:
<?php $count = 1; ?>
#foreach ($players as $player)
<tr>
<td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
</tr>
<?php $count++; ?>
#endforeach
Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.
If you want to use laravel 5 loop itration
`#foreach ($players as $player)
<tr>
<td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
</tr>
#endforeach`
Hope this helps.
What i used to do in order to avoid php tags in blade templates in 4.2 was something like that
#foreach ($players as $key => $player)
<tr>
<td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
#endforeach
It's kinda strange but it will do the trick.

Categories