Skip to content

Commit

Permalink
Merge pull request #57 from LiborStransky/master
Browse files Browse the repository at this point in the history
Add Model panel
  • Loading branch information
recca0120 committed Jan 8, 2021
2 parents f20448c + 7544daf commit 8c0e849
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions resources/views/ModelPanel/panel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<style class="tracy-debug">
#tracy-debug td.Laravel-DatabasePanel-sql { background: white !important }
#tracy-debug .Laravel-DatabasePanel-source { color: #BBB !important }
#tracy-debug .Laravel-DatabasePanel-hint code { color:#f00!important }
#tracy-debug .Laravel-DatabasePanel-hint { margin-top: 15px }
#tracy-debug .Laravel-DatabasePanel-explain { margin-top: 15px }
</style>

<h1>Models: <?php echo $total ?></h1>

<div class="tracy-inner">
<div class="tracy-inner-container">
<table>
<tr>
<th>Model Name</th>
<th>Count</th>
</tr>
<?php foreach ($models as $name => $count) { ?>
<tr>
<td>
<?php echo $name; ?>
</td>
<td>
<?php echo $count; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
5 changes: 5 additions & 0 deletions resources/views/ModelPanel/tab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<span title="Model">
<span class="tracy-label">
<?php echo 'Models:'.$total ?>
</span>
</span>
53 changes: 53 additions & 0 deletions src/Panels/ModelPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Recca0120\LaravelTracy\Panels;

use Illuminate\Support\Str;
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;

class ModelPanel extends AbstractSubscriablePanel implements IAjaxPanel
{
/**
* $models.
*
* @var array
*/
protected $models = [];

/**
* $total number of models.
*
* @var int
*/
protected $total = 0;

/**
* subscribe.
*/
protected function subscribe()
{
$events = $this->laravel['events'];
$events->listen('eloquent.*', function ($event, $models) {
if (Str::contains($event, 'eloquent.retrieved')) {
foreach ($models as $model) {
$class = get_class($model);
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
$this->total++;
}
}
});
}

/**
* getAttributes.
*
* @return array
*/
protected function getAttributes()
{
return [
'total' => $this->total,
'models' => $this->models,
];
}
}
1 change: 1 addition & 0 deletions src/Tracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct($config = [], BarManager $barManager = null, Bar $ba
'panels' => [
'routing' => false,
'database' => true,
'model' => false,
'view' => false,
'event' => false,
'session' => true,
Expand Down

0 comments on commit 8c0e849

Please sign in to comment.