dashboard profil petani
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Dashboard;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Models\Pertanian\Tanaman;
|
||||
use App\Models\Pertanian\SkorPetani;
|
||||
|
||||
class ProfilPetaniController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!Gate::allows('super_admin') && !Gate::allows('admin')) {
|
||||
abort(403);
|
||||
}
|
||||
$title = 'Dashboard Profil Petani';
|
||||
|
||||
$komoditas = Komoditas::all();
|
||||
|
||||
$petani = Petani::pluck('id');
|
||||
$lahan = Lahan::whereIn('petani_id', $petani)->get();
|
||||
|
||||
$petaniSummary = Petani::select(
|
||||
'komoditas_id',
|
||||
DB::raw('COUNT(*) as total')
|
||||
)
|
||||
->groupBy('komoditas_id')
|
||||
->get();
|
||||
$lahanSummary = Lahan::select(
|
||||
'komoditas_id',
|
||||
DB::raw('COUNT(*) as total',
|
||||
DB::raw('SUM(luas_lahan) as luas_lahan'))
|
||||
)
|
||||
->whereIn('petani_id', $petani)
|
||||
->groupBy('komoditas_id')
|
||||
->get();
|
||||
$tanamanSummary = Tanaman::select(
|
||||
'komoditas_id',
|
||||
DB::raw('COUNT(*) as total')
|
||||
)
|
||||
->whereIn('lahan_id', $lahan->pluck('id'))
|
||||
->groupBy('komoditas_id')
|
||||
->get();
|
||||
|
||||
$case = "
|
||||
CASE
|
||||
WHEN skor_kesiapan_adopsi >= 4.0 THEN 'Penggerak'
|
||||
WHEN skor_kesiapan_adopsi >= 3.0 THEN 'Perintis'
|
||||
WHEN skor_kesiapan_adopsi >= 2.0 THEN 'Pemerhati'
|
||||
WHEN skor_kesiapan_adopsi >= 1.0 THEN 'Pemula'
|
||||
ELSE 'Belum Lengkap'
|
||||
END
|
||||
";
|
||||
$skorSummary = SkorPetani::selectRaw("$case AS segmen, COUNT(*) AS total")
|
||||
->whereIn('petani_id', $petani)
|
||||
->groupByRaw($case)
|
||||
->get();
|
||||
|
||||
$skorKomoditasSummary = SkorPetani::query()
|
||||
->join('petani', 'petani_skor.petani_id', '=', 'petani.id')
|
||||
->selectRaw("
|
||||
petani.komoditas_id,
|
||||
$case AS segmen,
|
||||
COUNT(petani.id) AS total
|
||||
")
|
||||
->whereIn('petani_skor.petani_id', $petani)
|
||||
->groupBy('petani.komoditas_id')
|
||||
->groupByRaw($case)
|
||||
->get();
|
||||
|
||||
return view('dashboard.petani', compact('title',
|
||||
'komoditas',
|
||||
'petaniSummary',
|
||||
'lahanSummary',
|
||||
'tanamanSummary',
|
||||
'skorSummary',
|
||||
'skorKomoditasSummary'
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user