How do i count a specific column on the dashboard in Laravel? - 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>

Related

Auth::user on Laravel with different table

I have two additional column for login with different tables. So, I used 3 tables for login with output like this:
When I want to use username its simply write it like this {{ Auth::user()->username }} so I get value KASIR WEB. But it not work with kd_lokasi and kd_shift. Where I should register it? Or how I can get kd_lokasi and kd_shift value and paste it on view? Thank you for helping me.
Table :
$kd_lokasi = DB::table("tb_kasir_lokasi")->where("vTampil", 1)->get()
->pluck("nm_lokasi", "kd_lokasi");
$kd_shift = DB::table("tb_kasir_shift")->where("vTampil", 1)->get()
->pluck("nm_shift", "kd_shift");

Using Eloquent with Spaces in Table Columns

I am using Laravel and have a table that has columns with spaces. I can't change the names of the columns. I am trying to use Eloquent and have tried many things to get this to work but can't. I thought this would be the answer:
{{$provider->'Entity Type Code'}}
Thanks
You can use {} brackets
{{ $provider->{'Entity Type Code'} }}
Or use another variables
$variable_name = 'Entity Type Code';
{{ $provider->$variable_name }}

How to check if user has a record in other table with laravel eloquent?

I have three tables.
Users: id|name
Questions: id|name
Answers: id|user_id|question_id
Now i want to check if logged in user has answered any question or not.
This is my query in controller.
Question::orderBy('id','desc')->get();
Answer::whereUserId(Auth:id()->get();
Now how can i match it in view ?
I dont exactly understand what you mean by "match it in view".
but in your controller you could do something like:
$answered = App\Answers::where("user_id",auth()->user()->id)->get();
But just as a suggestion: I would make a hasMany Relation in the User Model that you just can call:
auth()->user()->answers;
//
Edited: If you want to handle this in the view, you could write:
#foreach($questions as $question)
#if($question->user_id == auth()->user()->id)
{{question->name}}
#endif
#endforeach
super not elegant. but probably what you mean.

Laravel query builder using hasMany output

This is my syntax in blade file :
{{ $product->filters->where("filter_id","10") }}
And my output for this looks like :
{"3":{"id":7153,"product_id":"1","filter_id":"10","data":"Kajaria","created_at":null,"updated_at":null}}
And I want to extract only a particular data i.e. Kajaria.
I'm getting confused in writing syntax.
I had already tried these syntaxes :
{{ $product->filters->where("filter_id","10")->data }}
{{ $product->filters->where("filter_id","10")['data'] }}
Try something like this:
{{ $product->filters->where("filter_id","10")->first()->data }}
The thing is $product is a collection and you need to get property of just one object, so first() method returns first object from a collection which you can use.
What I understand is that you want to get a filter for a product. So, try this.
Product::with('filters')->where('filter_id', 10)->first()->data;

count() "where" statement in Laravel blade?

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.

Resources