I try to get data from database by using laravel query builder and show the data. But getting below error "Undefined variable $user_session_detail". I give all the code below that i prepare.
//My raw query
SELECT ru.external_ref_no AS SID, usd.user_name AS Username, rs.servicecode AS Package, rc.clientdesc as Entity, rc.clientip as NAS_IP,
ROUND((ROUND((SUM(usd.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) AS Upload,
ROUND((ROUND((SUM(usd.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Download,
ROUND((ROUND((SUM(usd.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) + ROUND((ROUND((SUM(usd.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Total_Usage
FROM user_session_detail usd, radservice rs, radclient rc, radgroup rg, raduser ru
WHERE ru.username=usd.user_name AND rs.serviceid=usd.service_id AND rg.groupid=usd.group_id
AND usd.client_id=rc.clientid AND usd.SESSION_START_TIME > '2021-09-30 00.00.01' AND usd.SESSION_START_TIME < '2021-09-30 23.59.59'
GROUP BY usd.user_name
HAVING (ROUND((SUM(usd.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(usd.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 > 15
AND (ROUND((SUM(usd.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(usd.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 < 20;
//Laravel query builder query
public function nsuresecret(){
$user_session_detail = DB::table('user_session_detail')
->select(array('user_session_detail.*'), DB::raw('raduser.external_ref_no AS SID, user_session_detail.user_name AS Username, radservice.servicecode AS Package,
radclient.clientdesc as Entity, radclient.clientip as NAS_IP,
ROUND((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) AS Upload,
ROUND((ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Download,
ROUND((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) + ROUND((ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Total_Usage'))
->join('radservice', 'user_session_detai.service_id', '=', 'radservice.serviceid')
->join('radclient', 'user_session_detail.client_id', '=', 'radclient.clientid')
->join('radgroup', 'user_session_detail.group_id', '=', 'radgroup.groupid')
->join('raduser', 'user_session_detail.user_name', '=', 'raduser.username')
->whereBetween('user_session_detail.SESSION_START_TIME', ['2021-09-30 00.00.01', '2021-09-30 23.59.59'])
->groupBy('user_session_detail.user_name')
->havingRaw((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 > 15
AND (ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 < 20)
->get();
return view('reports.secretuserlist',compact('user_session_detail'));
}
//Blade code
<table class="table table table-bordered table-striped table-hover">
<thead>
<tr>
<th>SID</th>
<th>Username</th>
<th>Package</th>
<th>Entity</th>
<th>NAS_IP</th>
<th>Upload</th>
<th>Download</th>
<th>Total_Usage</th>
</tr>
</thead>
<tbody>
#foreach($user_session_detail as $usd)
<tr>
<td>{{ $usd->SID }}</td>
<td>{{ $usd->UserName }}</td>
<td>{{ $usd->Package }}</td>
<td>{{ $usd->Entity }}</td>
<td>{{ $usd->NAS_IP }}</td>
<td>{{ $usd->Upload }}</td>
<td>{{ $usd->Download }}</td>
<td>{{ $usd->Total_Usage }}</td>
</tr>
#endforeach
</tbody>
</table>
try in this way. dd($user_session_detail) beefore passing the data to view from your controller for check.
$user_session_detail = DB::table('user_session_detail')
->join('radservice', 'user_session_detai.service_id', '=', 'radservice.serviceid')
->join('radclient', 'user_session_detail.client_id', '=', 'radclient.clientid')
->join('radgroup', 'user_session_detail.group_id', '=', 'radgroup.groupid')
->join('raduser', 'user_session_detail.user_name', '=', 'raduser.username')
->whereBetween('user_session_detail.SESSION_START_TIME', ['2021-09-30 00.00.01', '2021-09-30 23.59.59'])
->groupBy('user_session_detail.user_name')
->select('user_session_detail.*', DB::raw('raduser.external_ref_no AS SID, user_session_detail.user_name AS Username, radservice.servicecode AS Package,
radclient.clientdesc as Entity, radclient.clientip as NAS_IP,
ROUND((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) AS Upload,
ROUND((ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Download,
ROUND((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024,2) + ROUND((ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024,2) AS Total_Usage'))
->havingRaw((ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 > 15
AND (ROUND((SUM(user_session_detail.FREE_UPLOAD_OCTETS)/1048576)))/1024 + (ROUND((SUM(user_session_detail.FREE_DOWNLOAD_OCTETS)/1048576)))/1024 < 20)
->get();
Change your $users variable to $user_session_detail
You should check if compact('user_session_detail') has key user_session_detail
Related
I have referral commission details and different levels of referral users I need to show that in the table with there are levels. Here I did coded but I'm not satisfied with the logic please let me know the logic is correct or wrong you see the below images for reference.
What i'm looking for
Here my table
What i did so far
Controller
public function inDirectCommission()
{
$user = User::where('id', 1)->first();
$user_id = $user->id;
$direct_commissions = Referral::where('parent_id', $user_id)->get();
foreach ($direct_commissions as $row) {
$second_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($second_level as $row) {
$third_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($third_level as $row) {
$forth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($forth_level as $row) {
$fifth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($fifth_level as $row) {
}
}
}
}
}
return view('site.user.dashboard.commission.in_direct_commission', compact('second_level', 'third_level', 'forth_level', 'fifth_level'));
}
Blade
<table id="example1"
class="table table-bordered table-striped dataTable dtr-inline direct-table"
role="grid" aria-describedby="example1_info">
<thead>
<tr>
<th>Date</th>
<th>Referal</th>
<th>Level</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach ($second_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>2<sup>nd</sup> Level</td>
<td>4000</td>
</tr>
#endforeach
#foreach ($third_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>3rd<sup>rd</sup> Level</td>
<td>3000</td>
</tr>
#endforeach
#foreach ($forth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>4<sup>th</sup> Level</td>
<td>2000</td>
</tr>
#endforeach
#foreach ($fifth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>5<sup>th</sup> Level</td>
<td>1000</td>
</tr>
#endforeach
</tbody>
</table>
dd of second level user
IMPORTANT: as per my comments under the question, this answer is to emphasize the idea only. Code samples and concepts might need to be adjusted to match your needs.
Assume there are users A,B,C,D,E and F and each has ids 1,2,3,4,5 and 6 respectively.
Also let's assume 'A' was a directly registered user. And:
A refers B to the website
B refers C to the website
C refers D to the website
D refers E to the website, and
E refers F to the website
With each user record, you can store their referral chain (ref_chain) in the database. At the time of user registration you can do this.
$a->ref_chain = null; // because he hasn't been referred by anyone
$b->ref_chain = '1'; // B (id = 2) is a direct referral of A (id = 1)
$c->ref_chain = '2-1'; // C (id = 3) is a direct referral of B (id = 2)
$d->ref_chain = '3-2-1'; // D (id = 4) is a direct referral of C (id = 3)
// similarly
$e->ref_chain = '4-3-2-1';
$f->ref_chain = '5-4-3-2-1';
Here, what you do is, at the time of registration you get the referring user's ref_chain and attach referring user's id to to the beginning of the string with a hyphen. For an example if we consider the time of C's registration:
$referringUser = $b;
$c->ref_chain = $b->id.'-'.$b->ref_chain;
Now consider the scenario where you want to get the referrals of C.
What you have to do is search in the ref_chain column in the database using the SQL like keyword;
$referralsOfC = User::where('ref_chain','like',$c->id.'-%')
->orWhere('ref_chain','like','%-'.$c->id.'-%')
->orWhere('ref_chain','like','%-'.$c->id)
->orWhere('ref_chain',"$c->id")
->get();
After that, you can loop through the collection and display the result as you please. Depending on the position of C's id in the ref_chain attribute you can recognize which level the referral is in.
This way you can drastically reduce the number of database queries and traversal.
Hope you can understand.
I am using laravel 5.7.19.
Within my TableController.php controller I query data from my db and hand it over to the view:
public function index()
{
$c = DB::table('tick_data')
->select('*')
->join('basis', 'basis.Id', '=', 'tick_data.b_id')
->whereRaw('tick_data.id IN( SELECT MAX(tick_data.id) FROM tick_data GROUP BY tick_data.exchange_timestamp)')
->get();
return view('datatable')->with('coins', $c);
}
Within my table.blade.php file I am putting the data out:
#foreach ($coins as $key=>$c)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $c->pair }}</td>
<td>{{ number_format($c->last_price, 8) }}</td>
<td>{{ number_format($c->price_change_percentage, 8) }}</td>
<td>{{ number_format($c->price_change, 8) }}</td>
<td>{{ number_format($c->high_price, 8) }}</td>
<td>{{ number_format($c->low_price, 8) }}</td>
<td>{{ $c->base_volume }}</td>
<td>{{ $c->name }}</td>
</tr>
#endforeach
As you can see I am mainly using the number_format() function for formatting my values.
However, base_volume comes in the form of 467703.0000000000 or 10831.13202978000 and I would like to change it to the shortform with K,M,B.
Is there any function in blade that can do this? What is a good way to preformat the numbers?
Appreciate your replies!
I don't know any blade functions that can do this. However, you can use the PHP Humanizer package for this. For example, from their documentation:
use Coduo\PHPHumanizer\NumberHumanizer;
NumberHumanizer::metricSuffix(-1); // "-1"
NumberHumanizer::metricSuffix(0); // "0"
NumberHumanizer::metricSuffix(1); // "1"
NumberHumanizer::metricSuffix(101); // "101"
NumberHumanizer::metricSuffix(1000); // "1k"
NumberHumanizer::metricSuffix(1240); // "1.2k"
NumberHumanizer::metricSuffix(1240000); // "1.24M"
NumberHumanizer::metricSuffix(3500000); // "3.5M"
The good thing about the package is, it also supports multiple locales.
Edit: There are also other alternative solutions as discussed here. This could be a better option for you if you just need to shorten numbers.
I have this foreach loop in my blade template.
#foreach ($employee->projects->groupBy('stage') as $stage => $project)
<tbody>
<td>{{ $stage }}</td>
<td class="text-center">{{ count($project) }}</td>
</tbody>
#endforeach
Where it finds all the projects that the employee is involved in and groups them by what stage they are in (Waiting, In Progress, Launched). And then it lists the stages and the count of how many are in each stage. This works perfectly but they are not in the right order. I would like them in Waiting, In Progress, Launched. Currently it's in Launched, Waiting, In Progress.
How can I order them in my custom order?
SQL should be
SELECT ... FROM projects GROUP BY stage
ORDER BY stage = 'Waiting' DESC, stage = 'In Progress', ...
probably you need use similar to this
->order_by(DB::expr("stage = 'Waiting' DESC"))->order_by(...)
Ok figured this out. Here's what I did...
$projects = $employee->projects()
->groupBy('stage')
->select('stage', DB::raw('count(*) as total'))
->orderByRaw('case
when stage = "Waiting" then 1
when stage = "In Progress" then 2
when stage = "Launched" then 3
end')
->get();
Then in my blade template, I did...
#foreach ($projects as $project)
<tbody>
<td>{{ $project->stage }}</td>
<td class="text-center">{{ $project->total }}</td>
</tbody>
#endforeach
Hope this helps someone else.
So I am currently working on a project where I have a table that shows an overview of managers and the number of applications they are assigned to that currently have a certain status. What I am trying to do is when the user clicks on a number (which is the count) it returns a list view that queries from the database ONLY those applications that are assigned to that manager and that status. Otherwise, if the user goes to the application view, it should show ALL applications.
Here is the code of the View for my overview:
<table class="table tablesorter" id="tblAffs">
<thead>
<tr class="tblhead">
<th>AM</th>
<th>Qualified Prospect</th>
<th>Unqualified Prospect</th>
<th>Contacted</th>
<th>Converted To Account</th>
<th>Pending Integration</th>
<th>Revenue Generating</th>
</tr>
</thead>
<tbody>
#foreach ($totalCount as $id => $name) {{-- id is the admin id --}}
#foreach($name as $n => $status) {{-- $n is the name, $status is array of the counts --}}
<tr>
<td>
{{$n}}
<br>
Closed
</td>
<td>{{ isset($status[2]) ? $status[2] : 0 }}</td>
<td>{{ isset($status[1]) ? $status[1] : 0 }}</td>
<td>{{ isset($status[3]) ? $status[3] : 0 }}</td>
<td>{{ isset($status[4]) ? $status[4] : 0 }}</td>
<td>{{ isset($status[5]) ? $status[5] : 0 }}</td>
<td>{{ isset($status[6]) ? $status[6] : 0 }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
On my controller for this overview this is the code and how data structure created:
public function overview()
{
$query= DB::table('admins')
->join('advertiser_applications', 'admins.id', '=', 'advertiser_applications.assigned_to')
->selectRaw('advertiser_applications.status, admins.id, admins.first_name, COUNT(advertiser_applications.status) count')
->groupBy('admins.id', 'advertiser_applications.status');
$counts = $query->get();
$totalCount = [];
foreach($counts as $count){
$totalCount[$count->id][$count->first_name][$count->status] = $count->count;
}
return View::make('admin.crm.overview', ['totalCount' => $totalCount, 'admin_id' => AuthAdmin::admin(false), 'tpl_title' => 'CRM Advertiser Overview', 'new_lead' => 'advertisers']);
}
Here is the code from my Controller for my view that brings up the list. This is where I am confused as to how I will pass in that status and id if that href is clicked.
public function index($param)
{
$query = AdvertiserApplication::with('admin');
$status = Input::get('status');
if (!empty($status) || $status === '0')
$query->where('status', '=', $status);
$applications = $query->get();
return View::make('admin.advertisers.application_list', ['applications' => $applications, 'admin_id' => AuthAdmin::admin(false)]);
}
Now I am not sure if that is the proper way to link that I have written in my href. Should I do a URL link to route or something like that? I am not sure yet. Any tips would be much appreciated!
With this your URL:
{{ isset($status[2]) ? $status[2] : 0 }}
If you have
route::get('/ap1/{new_lead}', array('as' => 'page-name', 'uses' => 'yourController#functionName'));
in controller:
public function functionName($new_lead){
// use $new_lead here to get your data
}
I have this function on a repository:
public function getSolicitudes($usuario_id)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('su.id, su.fecha_creacion, tt.nombre AS tipo_tramite, tr.nombre AS tipo_registro, es.nombre AS estado_solicitud')
->from("ComunBundle:SolicitudUsuario", "su")
->where('su.usuario = :usuario_id')
->join('su.tipo_tramite', 'tt', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->join('su.tipo_registro', 'tr', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->join('su.estado_solicitud', 'es', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->orderBy('su.fecha_creacion', 'DESC')
->setParameter('usuario_id', $usuario_id);
return $qb->getQuery()->getResult();
}
In Twig template I have this:
{{ entities|ladybug_dump }}
And the output is something like image show:
I'm trying to iterate over the result in Twig template as follow:
{% for solicitud in entities %}
<tr>
<td></td>
<td>{{ solicitud.tramite }}</td>
<td>{{ solicitud.id }}</td>
<td>{{ solicitud.solicitud }}</td>
<td>{{ solicitud.estado }}</td>
<td>{{ solicitud.fecha }}</td>
<td></td>
</tr>
{% endfor %}
But I got this error:
Key "tramite" for array with keys "id, fecha_creacion, tipo_tramite,
tipo_registro, estado_solicitud" does not exist in
/var/www/html/src/RPNIBundle/Resources/views/Listado/index.html.twig
at line 25
What I'm missing here?
It's obvious, you're using the wrong key, as the exception suggests:
Key "tramite" for array with keys "id, fecha_creacion, tipo_tramite, tipo_registro, estado_solicitud" does not exist in /var/www/html/src/RPNIBundle/Resources/views/Listado/index.html.twig at line 25
Your object has a tipo_tramite key, not tramite. You should change the output statements.