62 lines
2.1 KiB
PHP
62 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ContentBrief;
|
|
use App\Models\SocialStat;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class SocialHubController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
// Get latest stats per platform
|
|
$stats = collect(['instagram', 'tiktok', 'youtube'])->map(fn (string $platform) => [
|
|
'platform' => $platform,
|
|
'latest' => SocialStat::where('platform', $platform)
|
|
->orderByDesc('recorded_date')
|
|
->first(),
|
|
'history' => SocialStat::where('platform', $platform)
|
|
->orderByDesc('recorded_date')
|
|
->limit(7)
|
|
->get(),
|
|
]);
|
|
|
|
// Posting frequency - count by platform per month
|
|
$postingFrequency = ContentBrief::where('status', 'posted')
|
|
->selectRaw("platform, strftime('%Y-%m', posted_date) as month, count(*) as count")
|
|
->whereNotNull('posted_date')
|
|
->groupBy('platform', 'month')
|
|
->orderBy('month')
|
|
->limit(30)
|
|
->get();
|
|
|
|
return view('social-hub.index', compact('stats', 'postingFrequency'));
|
|
}
|
|
|
|
public function storeStats(Request $request): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'platform' => 'required|in:instagram,tiktok,youtube',
|
|
'handle' => 'nullable|string|max:100',
|
|
'followers' => 'required|integer|min:0',
|
|
'following' => 'nullable|integer|min:0',
|
|
'posts_count' => 'nullable|integer|min:0',
|
|
'engagement_rate' => 'nullable|numeric|min:0|max:100',
|
|
'avg_views' => 'nullable|integer|min:0',
|
|
'avg_likes' => 'nullable|integer|min:0',
|
|
'avg_comments' => 'nullable|integer|min:0',
|
|
]);
|
|
|
|
$validated['recorded_date'] = today()->toDateString();
|
|
|
|
SocialStat::create($validated);
|
|
|
|
return back()->with('success', '📊 Stats updated!');
|
|
}
|
|
}
|