I am currently working on a Laravel project and run the following query to build this multidimensional array:
Basically what I am trying to do is see the 5 different statuses and get the count of how many of those applications with a certain status are assigned to a certain admin.
The problem I am running into is outputting this data on my view. This is what I have so far:
I am getting an error saying undefined index first_name.
Any help would be much appreciated!
EDIT: Here is the output of var_dump($totalCounts);
The reason that you get undefined index is that you don't assign an exact index first_name.
Lets say the user's first name is Burak, status is Something and count is 5 within the line below.
$totalCount[$count->first_name][$count->status] = $count->count;
And actually you assign like below.
$totalCount['Burak']['Something'] = 5;
So if you want to access to it, then you need to reach to the first name within your view. To access them within your view :
#foreach ($totalCount as $name => $count)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#for($i = 1; $i <= 6; $i++)
<td><a href="#">
#foreach ($count as $status => $c)
#if($i == $status)
{{ $c }}
#endif
#endforeach
</a></td>
#endfor
</tr>
#endforeach
Related
I'm trying to show a list of mssg, but I get the following error:
Trying to access array offset on value of type bool
#foreach ($mssg as $mssg)
<tr>
<td>{{$mssg["objMessage"]}}</td>
<td>{{ $mssg["estDateMessage"] }} </td>
<td>
#if($mssg["active"]==0)
{{"message non lue"}}
#else
{{"messaage lue"}}
#endif
</td>
<td><button>lire</button></td>
<td><button>delete</button></td>
</tr>
#endforeach
This is my controller
function indexe() {
$x = Auth::User()->id;
$mess = Auth::User()->name;
$mssg = messages::find($x);
foreach($mssg as $m) {
dd($m["idUser"]);
}
return view('liate_message',["mssg"=>$mssg],["idd"=>$mess]);
}
Check your foreach code
#foreach ($mssg as $mssg)
change to
#foreach ($mssg as $m)
then change all entries like {{$mssg["objMessage"]}} to {{$m["objMessage"]}}
You can change the $m to any variable names you want.
Make sure variable $mssg => not null and is_array.
You should dd the $mssg variable in the controller to check what it is, before returning $mssg to the view.
I have a forum I'm building and replies are always incrementing with their unique id's.
This means that I can have a thread and have the reply number at 550 when the thread starts, not displaying the correct reply number in relation to the thread.
Is there anyway that when I retrieve these records, I can reset them to 1 and have them increment respective to their threads?
Thanks for any help.
Here is what I have so far:
#for($i = 0; $i < $thread->replies->count(); $i++)
Post {{ $i }} of {{ $thread->replies->count() }}
#endfor
It seems to work except this is stuck in my $replies as $reply loop and it proliferates all records.
I would use a #foreach loop and the $loop variable to do the counting, See the $loop doc
#foreach($thread->replies as $post)
Post {{ $loop->iteration }} of {{ $loop->count }}
#endfor
In the case of spanning multiple pages, you should implement Pagination instead See the Pagination doc
{{ ($replies->currentPage()-1) * $replies->perPage() + $loop->index + 1 }} of {{ $replies->total() }}
Found here: Laravel - Use $loop->iteration with pagination
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
In my blade.php file, I'm trying to merge two foreach loop to display data in the table. I had tried use something like nested loop but it doesn't work.
#foreach($cat->products as $idx => $product)
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
</tr>
#endforeach
#endforeach
I just need to insert the total data into the table. Now the total is displayed correctly but it will duplicate the sku and name in the table.
Define an array which we will use as a "flag" and on each iteration check if the current product SKU is NOT in our "flag" array. If it isn't then we display and we add the current product SKU to the list of processed SKUs and continue as normal.
#php $skus = [] #endphp
#foreach($cat->products as $idx => $product)
#if (!in_array($product->sku, $skus))
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
#endforeach
</tr>
#php array_push($skus, $product->sku); #endphp
#endif
#endforeach
Note: You are using Laravel 4, the current version of Laravel is 5.7, I would absolutely update and use the latest version.
Reading Material
in_array
array_push
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.