git add .
git commit "fix dashboard charts rendering and views data display"
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Admin;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Tour;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
@@ -21,28 +22,37 @@ class DashboardController extends Controller
|
||||
|
||||
$flightsCount = Flight::count();
|
||||
|
||||
$tourViews= Tour::query()
|
||||
->selectRaw('DATE(created_at) as date,SUM(views) as total')
|
||||
->where('created_at','>=',now()->subDays(9))
|
||||
->groupBy('date')
|
||||
->orderBy('date')
|
||||
->get();
|
||||
$tourViews = $this->getViewsForLast10Days(Tour::class, 'tour_views');
|
||||
|
||||
$flightViews=Flight::query()
|
||||
->selectRaw('DATE(created_at) as date,SUM(views) as total')
|
||||
->where('created_at','>=',now()->subDays(9))
|
||||
->groupBy('date')
|
||||
->orderBy('date')
|
||||
->get();
|
||||
$flightViews = $this->getViewsForLast10Days(Flight::class, 'flight_views');
|
||||
|
||||
$logs = ActivityLog::with('admin')
|
||||
->latest()
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
return view('admin.dashboard',compact('adminsCount',
|
||||
'usersCount','toursCount','flightsCount','tourViews',
|
||||
'flightViews','logs'
|
||||
return view('admin.dashboard', compact(
|
||||
'adminsCount',
|
||||
'usersCount', 'toursCount', 'flightsCount', 'tourViews',
|
||||
'flightViews', 'logs'
|
||||
));
|
||||
}
|
||||
|
||||
private function getViewsForLast10Days(string $model, string $cachePrefix)
|
||||
{
|
||||
$ids = $model::pluck('id');
|
||||
|
||||
return collect(range(9, 0))->map(function ($i) use ($ids, $cachePrefix) {
|
||||
$date = now()->subDays($i)->format('Y-m-d');
|
||||
|
||||
$total = $ids->sum(function ($id) use ($cachePrefix, $date) {
|
||||
return Cache::get("{$cachePrefix}:{$id}:{$date}", 0);
|
||||
});
|
||||
|
||||
return [
|
||||
'date' => verta($date)->format('m/d'),
|
||||
'total' => (int) $total,
|
||||
];
|
||||
})->values();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +87,6 @@ class TourController extends Controller
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function edit(Tour $tour)
|
||||
{
|
||||
return view('admin.tours.edit',compact('tour'));
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Tour;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FlightController extends Controller
|
||||
{
|
||||
@@ -40,6 +41,9 @@ class FlightController extends Controller
|
||||
{
|
||||
$flight->increment('views');
|
||||
|
||||
$cacheKey = 'flight_views:' . $flight->id . ':' . now()->format('Y-m-d');
|
||||
Cache::put($cacheKey, Cache::get($cacheKey, 0) + 1, now()->addDays(15));
|
||||
|
||||
$comments = $flight->comments()
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Tour;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class TourController extends Controller
|
||||
{
|
||||
@@ -24,6 +25,10 @@ class TourController extends Controller
|
||||
{
|
||||
$tour->load('travelType');
|
||||
$tour->increment('views');
|
||||
|
||||
$cacheKey = 'tour_views:' . $tour->id . ':' . now()->format('Y-m-d');
|
||||
Cache::put($cacheKey, Cache::get($cacheKey, 0) + 1, now()->addDays(15));
|
||||
|
||||
$comments = $tour->comments()
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
@@ -174,46 +174,98 @@
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
|
||||
<script>
|
||||
const tourChart = document.getElementById('tourViewsChart');
|
||||
new Chart(tourChart, {
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
|
||||
|
||||
const tourData = @json($tourViews);
|
||||
|
||||
const flightData = @json($flightViews);
|
||||
|
||||
|
||||
|
||||
new Chart(document.getElementById('tourViewsChart'), {
|
||||
|
||||
type: 'bar',
|
||||
|
||||
data: {
|
||||
|
||||
labels: @json($tourViews->pluck('date')),
|
||||
labels: tourData.map(item => item.date),
|
||||
|
||||
datasets: [{
|
||||
|
||||
label: 'بازدید تورها',
|
||||
|
||||
data: @json($tourViews->pluck('total'))
|
||||
data: tourData.map(item => item.total),
|
||||
|
||||
}]
|
||||
|
||||
},
|
||||
|
||||
options: {
|
||||
|
||||
responsive: true,
|
||||
|
||||
scales: {
|
||||
|
||||
y: {
|
||||
|
||||
beginAtZero: true
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
const flightChart = document.getElementById('flightViewsChart');
|
||||
new Chart(flightChart, {
|
||||
|
||||
|
||||
|
||||
new Chart(document.getElementById('flightViewsChart'), {
|
||||
|
||||
type: 'bar',
|
||||
|
||||
data: {
|
||||
|
||||
labels: @json($flightViews->pluck('date')),
|
||||
labels: flightData.map(item => item.date),
|
||||
|
||||
datasets: [{
|
||||
|
||||
label: 'بازدید پروازها',
|
||||
|
||||
data: @json($flightViews->pluck('total'))
|
||||
data: flightData.map(item => item.total),
|
||||
|
||||
}]
|
||||
|
||||
},
|
||||
|
||||
options: {
|
||||
|
||||
responsive: true,
|
||||
|
||||
scales: {
|
||||
|
||||
y: {
|
||||
|
||||
beginAtZero: true
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
</script>
|
||||
@endsection
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
</script>@endsection
|
||||
|
||||
|
||||
|
||||
+1
-2
@@ -26,8 +26,7 @@ Route::prefix('admin')->name('admin.')->group(function (){
|
||||
Route::get('/','login')->name('index');
|
||||
Route::post('/','loginPost')->name('post');
|
||||
});
|
||||
Route::post('logout','logout')->middleware('auth:admin')
|
||||
->name('logout');
|
||||
Route::post('logout','logout')->middleware('auth:admin')->name('logout');
|
||||
});
|
||||
|
||||
Route::middleware('auth:admin')->group(function (){
|
||||
|
||||
Reference in New Issue
Block a user