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.
Related
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
Introduction to the problem
I have written a class to import a specific CSV file. This class handles everything except moving and reading the imported file. This is done in the controller by using de File facade in Laravel.
The description below only describes one of the arrays, but I use more arrays, and also in the class itself I get the same problem. The problem is that the app crashes with some obscure error everytime I try to access an item in the array by using a string as key value. I tested this in a seperate, non-laravel, php-file and there I don't get an error. It's only in my Laravel app. Strange thing is that I tested the arrays the following way:
a) using array_key_exists - No keys are found (I used both ' and ")
b) using array_keys - This time I get all the keys from the array
c) for example: $report['student_number'] - Doesn't work, I get an error here
d) for example: $report[array_keys($report)[0]] - This works
e) In vanilla PHP I do the same and there I do not get any errors.
The real code
In the controller I handle the upload the following way:
$uploaded_file = $request->file('uploadfile');
$uploaded_file->move(storage_path().'/files', $uploaded_file->getClientOriginalName());
$import_file = storage_path().'/files/'.$uploaded_file->getClientOriginalName();
if(File::exists($import_file) && File::isReadable($import_file)) {
$raw_file_data = File::get($import_file);
$import = ImportStudents::getInstance();
$import_result = $import->import($raw_file_data);
}
In the Import class I have a private property:
private $report = [];
I fill the array with data the following way:
$this->report[$student_number][] = [
'field' => $field,
'db_field' => 'student_number',
'new_value' => $value,
'old_value' => $student_number,
'action' => 'updated'
];
In the class I finaly return this array to the controller:
return $this->report;
In the controller I pass this array to the view:
return view('home.import_report')->with('report', $import_result);
In the blade file I use the following code:
#foreach($report as $report_row)
<td>{{ $report_row['field'] }}</td>
<td>{{ $report_row['db_field'] }}</td>
<td>{{ $report_row['new_value'] }}</td>
<td>{{ $report_row['old_value'] }}</td>
<td>{{ $report_row['action'] }}</td>
#endforeach
The error
The "C:\Windows\Temp\php57CD.tmp" file does not exist or is not
readable.
Remarks
Above is only one part in my code where I get the error. As I mentioned before, I also get the same error in the code of the class.
Specifications
Laravel: 6.x
PHP: 7.3.10
Question
What am I overlooking here? What is the problem?
The solution for the above mentioned problem with showing the report, how could I forget, that I needed an extra foreach loop.
Old code
#foreach($report as $report_row)
<td>{{ $report_row['field'] }}</td>
<td>{{ $report_row['db_field'] }}</td>
<td>{{ $report_row['new_value'] }}</td>
<td>{{ $report_row['old_value'] }}</td>
<td>{{ $report_row['action'] }}</td>
#endforeach
The answer was so obvious already in my own code :-(, this code:
$this->report[$student_number][] = [
'field' => $field,
'db_field' => 'student_number',
'new_value' => $value,
'old_value' => $student_number,
'action' => 'updated'
];
New code (solution)
#forelse($report as $student_number => $report_row)
<tr><td colspan="6" style="color: white; background-color: darkblue; padding: 0;"><h5>{{ $student_number }} - {{ $report_row[0]['name'] }}</h5></td></tr>
#foreach($report_row as $actions)
<tr>
<td></td>
<td>{{ $actions['field'] }}</td>
<td>{{ $actions['db_field'] }}</td>
<td>{{ $actions['new_value'] }}</td>
<td>{{ $actions['old_value'] }}</td>
<td>{{ $actions['action'] }}</td>
</tr>
#endforeach
#empty
<tr><td colspan="6">No changes</td></tr>
#endforelse
But this problem still exists with arrays
a) using array_key_exists - No keys are found (I used both ' and ")
b) using array_keys - This time I get all the keys from the array
c) for example: $report['student_number'] - Doesn't work, I get an error here
d) for example: $report[array_keys($report)[0]] - This works
e) In vanilla PHP I do the same and there I do not get any errors.
What's not working
$student->first_name = $imported_row['Roepnaam'];
But the key 'Roepnaam' does exist in the array.
With array_keys($imported_row) I can see that the key exists.
What is working
$student->first_name = $imported_row[array_keys($imported_row)[1]];
This what puzzles me!
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.
Bit of a bad title, not quite sure how to research/describe what I'm trying to do.
I have a table called specifications with the fields
id, device_id, name, detail
Two rows for two different devices would for example be
1, 1, weight, 300kg
2, 2, weight, 250kg
Now the way I need to display it is in a single table compare both devices, essentially like this:
#foreach($specifications as $specification)
<tr>
<td>{{ $specification->name }}</td>
<td>{{ $specification->detailOne }}</td>
<td>{{ $specification->detailTwo }}</td>
</tr>
#endforeach
I'm having issues getting the right query or split the query in order to be able to go through the array like above. Anyone mind putting me on the right mindset? Do I query both specs and somehow resort it into an array I can use as above, or is there something else I should be looking at?
This should work for you
#foreach($specifications->groupBy('name') as $name => $specificationGrouped)
<tr>
<td>{{ $name }}</td>
#foreach($specificationGrouped as $specification)
<td>{{ $specification['detail'] }} ({{ $specification['device_id'] }})</td>
#endforeach
</tr>
#endforeach
I have a bidirectional one-to-many relationship between two classes (Protocol and History). While searching for a specific Protocol, I'm expected to see all History entries associated with that protocol.
While rendering my template, I pass the following:
return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'history' => $entity->getHistory(),
)
);
entity->getHistory() returns a PersistentCollection instead of an array, which causes the following to render an error:
{% for hist in history %}
<tr>
<td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td>
<td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td>
</tr>
{% endfor %}
If instead of $entity->getHistory() I pass $em->getRepository('MyBundle:History')->findByProtocol($entity), it works fine. But I figure the main point of having a bidirectional relationship was to avoid opening repositories and explicitly opening new resultsets.
Am I doing something wrong? How am I supposed to do that?
All I had to do was call the following on my TWIG:
{% for hist in entity.history %}
None of the other answers worked for me. I have to call the property directly in my twig instead of using its getter. Don't know why, but it worked.
Thanks.
Try this:
return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig',
array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'history' => $entity->getHistory()->toArray()
)
);
Your code is fine, I always save myself the trouble and get my collections in the twig instead of passing it through my view. You could try this too.
Render code change
return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
)
);
You want to access the history directly in your twig.
Twig
{% for hist in entity.getHistory() %}
<tr>
<td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td>
<td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td>
</tr>
{% endfor %}
If your result after the change is the same, try checking hist for an array, it could be nested! Persistent Collections tend to do that...
{% for history in entity.getHistory() %}
{% for hist in history %}
<tr>
<td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td>
<td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td>
</tr>
{% endfor %}
{% endfor %}