I have the following foreach loop that I'm getting good results with however when it gets to the elseif statement I'm attempting to see if two of the players are on the same team or not and if they are then have them listed together separated with a & symbol. Currently I am getting the following still.
Player 1 vs. Player 2 vs. Player 3 vs. Player 4
This is okay however Player 1 and Player 2 are on the same team. So its not seeing them as on the same team for some reason. Does someone see what my issue is?
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($match->players->count() - 1 != $key)
vs.
#elseif ($match->players[$key - 1]->team_id == $player->team_id)
&
#endif
#endforeach
EDIT: I changed the data a little but still should work.
http://pastebin.com/AdyzemC4
It was tricky to puzzle together your $match variable :)
#if($match->players->count() - 1 != $key)
Your $match->players->count() is always equal to the value of the number of players(say 'n'). So your 'if' ONLY checks the 'n-1' != current key ($key). [Which is true except for the last key, so you get all 'vs'].
This should work:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endforeach
In this we are checking if the current player's team is the same as the next player's team [$key+1].
Note:
You need to stop the loop for the last player, since $key+1 will go outside your array and you will get an offset error.
Therefore add another if:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($key + 1 < $match->players->count())
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endif
#endforeach
Related
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
I have a project where I'm pulling posts from the database and rendering on the home page view. Like a Blog. (On the home page I limit this to 3 posts)
I have 6 rows in the table, and would like to style the output based on ODD and Even rows.
Here is my controller:
public function index()
{
$counter = Post::count();
$posts= DB::table('posts')->orderBy('id', 'DESC')->limit(3)->get();
return view('home',compact('posts','counter'));
}
I want the even numbered rows to have <div class="even"> and the odd numbered rows to have <div class="odd">
When I dd on $counter I get the value 6. This is correct. I have 6 rows in the table.
What I'm currently trying based on other articles I've found:
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
This doesn't do anything. Still outputs the rows as 6,5,4,3,2,1
So how can I write the IF Statement inside my Foreach loop to say...
if ($counter == odd)
<div class="odd">
else
<div class="even">
The order I'm looking for is:
Odd
Even
Odd
Even
$counter is a static variable, so calling $counter % 2 == 0 will always show the same result.
If you are using Laravel 5.4+, there is a $loop variable included in the #foreach(). So you can access your mod division within the loop.
Here is the example for Laravel 5.4+
#foreach ($posts as $post)
#if($loop->iteration % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#endforeach
Laravel 5.8.5 add even and odd Boolean flags in the Blade loop variable
Now you can use:
$loop->even or $loop->odd
Instead of
$loop->iteration % 2
Reference link
You need to increment the counter on each iteration.
#php
$counter = 1;
#endphp
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#php
$counter++;
#endphp
#endforeach
table(skill)
have many skills, columns type boolean(0,1),by laravel
i want to get the names of columns if skills value equal 1 and user_id equal 1,
without say the name of column becouse they 50 skills ,i tried this on sublime
<?php
$variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
#foreach($variable2 as $key1 => $value1)
#if($value1='1')
<span class="tags">{{ $key1 }}</span>
#endif
#endforeach
Well, looks like you forgot, that get returns a collection of skills. You must use two loops.
<?php
$userSkills = App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->get();
?>
<!-- loop through a list of user skills (get gives us a set of skills -->
#foreach($userSkills as $skill)
<!-- loop through skill properties -->
#foreach($skill->toArray() as $key => $value)
#if($value)
<span class="tags">{{ $key }}</span>
#endif
#endforeach
#endforeach
<?php $variable2=App\Skill::where('user_id',Auth::user()->id)
->where('job_id','null')
->where('user_id', 1)
->get();
$skills = $variable2->filter(function ($item) { return $item === 1; })->keys();
?>
#foreach($skills as $key)
<span class="tags">{{ $key }}</span>
#endforeach
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
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.