count() "where" statement in Laravel blade? - laravel

I am interested in displaying quick counts of email stats in my application, but I'm getting hung up on finding an efficient way to generate the counts. I am hoping to just use the eloquent relationship with some sort of "count where" statement inside of blade.
Getting the total count works as it should:
{{count($emails->mandrillemails)}}
but is something like this possible?:
{{count($emails->mandrillemails->msg_state == 'bounced')}}
{{count($emails->mandrillemails->msg_state == 'open')}}
Here is my block of code with the #if and #foreach statements:
#if(count($sentEmails) > 0)
#foreach ($sentEmails as $emails)
<tr>
<td>
#if(count($emails->mandrillemails) > 0)
<p><span class="badge"><i class="fa fa-paper-plane"></i> {{count($emails->mandrillemails)}}</span> <span class="badge"><i class="fa fa-exclamation-triangle"></i> {{count($emails->mandrillemails->msg_state == 'bounced')}}</span></p>
#endif
</td>
</tr>
When I attempt this, I get an undefined property error on "msg_state"

You can count a relationship like this:
$emails->mandrillemails()->whereMsgState('bounced')->count();
Since you're #foreaching things, you may want to eager load this data. This is a little more complex when you're doing counts on the relationship - see http://laravel.io/forum/05-03-2014-eloquent-get-count-relation and http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/ for some possible techniques to eager-load the count value instead of the whole list of related items.

$emails->mandrillemails()->whereMsgState('bounced')->count() should work.

Related

How do i count a specific column on the dashboard in Laravel?

Below is how i view count of on my dashboard for all devices i store, where 'devices' are all devices am storing in my database. l want to view a count using a specific column name in a row. how do i do that!, any help?
#inject('devices', 'App\Device')
<div class="number"><strong>{{ $devices->count() }}</strong></div>
Are you looking for this?
$devices->where('column', 'matches')->count();
Or something like this maybe..
$devices->select('yourColumn', DB::raw('count(*) as total'))->groupBy('yourColumn')->get()
So i found out that i had to do something like this:
{{ $devices->where('Type_ID',
'Laptop')->count() }}
from: <div class="number"><strong>{{ $devices->count() }}</strong></div>
to something like this and it worked perfectly:
<div class="number"><strong>{{ $devices->where('Type_ID', 'Laptop')->count() }}</strong></div>

How to get only one element from collection in the view

I passed $users variable to my view and I want to get only one element of this collection by comparing it using contains() method.
It's working but I want to retrieve this element so I can show the name
how to do that?
My code:
#foreach($rqs as $rq)
<td>{{$rq->user_id}} with name:
#if($users->contains('id',$rq->user_id))
Working >> I want to get the user name here but only one user
#endif
</td>
#endforeach
What about:
$users->where('id',$rq->user_id)->first()->name

Elegant way to get 5 counts from the same query in Laravel

I have a query that gives me 200 results in 5 differents categories.
What I need is getting count of each categories.
What is the best way to do, make 5 counts or get the complete list, and process it ( it seems more heavy if I have to do a foreach)
Tx!
Laravel is awesome !
https://laravel.com/docs/5.1/collections#method-groupby
As I need the full list, I just get it once, and then I can apply groupby to a collection!
$groups = $users->groupBy('categoryId');
And then I just do
#foreach($groups as $group)
{{ sizeof($group) }}
#endforeach

Selecting images inside nested tables using Watir

I'm trying to use Watir to scrape a page which has AJAX-generated tables. As each level of the table is expanded, additional nested tables are inserted and the expand link moves in one level. The number of levels is not always the same. The end result is something like this, loaded in asynchronously one layer at a time:
<table>
Data Type 1
<table>
<table>
<table>
<img src="expand.gif" />
</table>
</table>
</table>
</table>
<table>
Data Type 2
<table>
<table>
<table>
<img src="expand.gif" />
<img src="expand.gif" />
</table>
</table>
</table>
</table>
I want to interact only with the table containing "Data Type 1." The table IDs are randomly generated (this is an annoying ASP.net application), so the only thing to select on is "Data Type 1."
Thus, I have:
while browser.table(:text => /Type 1/).image(:src => "expand.gif").present?
links = browser.table(:text => /Type 1/).images(:src => "expand.gif")
links.each do |li|
li.click
end
end
This works fine to expand the first nested table (with the expand.gif directly inside the table element that contains the selector text. However, the next layer of tables does not expand--clearly, my selector isn't going into child tables.
If I leave out the table selector altogether, the code will happily expand every nested table on the page until it runs out of levels, but I only want "Data Type 1."
Any suggestions how to accomplish this? Thanks in advance.

How to get the 1st column for all rows within a table using Selenium java API

I got a table with <tr> data as:
<tr class="iceDatTblRow1" id="body-subview:myMainPage:MainTabs:0:dataTable:0">
<td class="iceDatTblCol1"><span class="iceOutTxt" id="body-
subview:myMainPage:MainTabs:0:dataTable:0:j_idt252">Data that I want</span>
</td>
</tr>
for some reason, i can't seem to locate the <td> with id attribute within it...
please share your expertise - thanks,
I don't know about Selenium but there are many XPath solutions here. A few examples:
span everywhere:
//span[#id='body-subview:myMainPage:MainTabs:0:dataTable:0:j_idt252']
span only inside td:
//td/span[#id='body-subview:myMainPage:MainTabs:0:dataTable:0:j_idt252']
If your requirement is to get all the first column for all rows with Selenium and Xpath then you can try this code:
String tdLocator="//tr"
int count = selenium.getXpathCount(tdLocator)
for(int i = 1;i<=count;i++)
{
get to td by using //tr[i]/td[0]
}
If you have a table ID then you can also use
selenium.getTable("table_id.[rownumber].[colnumber])

Resources