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\Flight;
|
||||||
use App\Models\Tour;
|
use App\Models\Tour;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class DashboardController extends Controller
|
class DashboardController extends Controller
|
||||||
{
|
{
|
||||||
@@ -21,28 +22,37 @@ class DashboardController extends Controller
|
|||||||
|
|
||||||
$flightsCount = Flight::count();
|
$flightsCount = Flight::count();
|
||||||
|
|
||||||
$tourViews= Tour::query()
|
$tourViews = $this->getViewsForLast10Days(Tour::class, 'tour_views');
|
||||||
->selectRaw('DATE(created_at) as date,SUM(views) as total')
|
|
||||||
->where('created_at','>=',now()->subDays(9))
|
|
||||||
->groupBy('date')
|
|
||||||
->orderBy('date')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$flightViews=Flight::query()
|
$flightViews = $this->getViewsForLast10Days(Flight::class, 'flight_views');
|
||||||
->selectRaw('DATE(created_at) as date,SUM(views) as total')
|
|
||||||
->where('created_at','>=',now()->subDays(9))
|
|
||||||
->groupBy('date')
|
|
||||||
->orderBy('date')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$logs = ActivityLog::with('admin')
|
$logs = ActivityLog::with('admin')
|
||||||
->latest()
|
->latest()
|
||||||
->take(5)
|
->take(5)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return view('admin.dashboard',compact('adminsCount',
|
return view('admin.dashboard', compact(
|
||||||
'usersCount','toursCount','flightsCount','tourViews',
|
'adminsCount',
|
||||||
'flightViews','logs'
|
'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)
|
public function edit(Tour $tour)
|
||||||
{
|
{
|
||||||
return view('admin.tours.edit',compact('tour'));
|
return view('admin.tours.edit',compact('tour'));
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
|||||||
use App\Models\Flight;
|
use App\Models\Flight;
|
||||||
use App\Models\Tour;
|
use App\Models\Tour;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class FlightController extends Controller
|
class FlightController extends Controller
|
||||||
{
|
{
|
||||||
@@ -40,6 +41,9 @@ class FlightController extends Controller
|
|||||||
{
|
{
|
||||||
$flight->increment('views');
|
$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()
|
$comments = $flight->comments()
|
||||||
->latest()
|
->latest()
|
||||||
->get();
|
->get();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Tour;
|
use App\Models\Tour;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class TourController extends Controller
|
class TourController extends Controller
|
||||||
{
|
{
|
||||||
@@ -24,6 +25,10 @@ class TourController extends Controller
|
|||||||
{
|
{
|
||||||
$tour->load('travelType');
|
$tour->load('travelType');
|
||||||
$tour->increment('views');
|
$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()
|
$comments = $tour->comments()
|
||||||
->latest()
|
->latest()
|
||||||
->get();
|
->get();
|
||||||
|
|||||||
@@ -174,46 +174,98 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
|
|
||||||
<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',
|
type: 'bar',
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
|
|
||||||
labels: @json($tourViews->pluck('date')),
|
labels: tourData.map(item => item.date),
|
||||||
|
|
||||||
datasets: [{
|
datasets: [{
|
||||||
|
|
||||||
label: 'بازدید تورها',
|
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',
|
type: 'bar',
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
|
|
||||||
labels: @json($flightViews->pluck('date')),
|
labels: flightData.map(item => item.date),
|
||||||
|
|
||||||
datasets: [{
|
datasets: [{
|
||||||
|
|
||||||
label: 'بازدید پروازها',
|
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::get('/','login')->name('index');
|
||||||
Route::post('/','loginPost')->name('post');
|
Route::post('/','loginPost')->name('post');
|
||||||
});
|
});
|
||||||
Route::post('logout','logout')->middleware('auth:admin')
|
Route::post('logout','logout')->middleware('auth:admin')->name('logout');
|
||||||
->name('logout');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::middleware('auth:admin')->group(function (){
|
Route::middleware('auth:admin')->group(function (){
|
||||||
|
|||||||
Reference in New Issue
Block a user