petani lahan
This commit is contained in:
@@ -5,12 +5,17 @@ namespace App\Http\Controllers\Web\Produktivitas;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Models\Wilayah\DesaKelurahan;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Pertanian\Tanaman;
|
||||
|
||||
class PetaniLahanController extends Controller
|
||||
{
|
||||
@@ -24,7 +29,12 @@ class PetaniLahanController extends Controller
|
||||
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate($request->per_page ?? 10);
|
||||
->get();
|
||||
|
||||
if ($lahan->count() > 0) {
|
||||
return redirect()->route('petani.lahan-tanaman.edit', [$petaniId, $lahan->first()->id]);
|
||||
}
|
||||
|
||||
|
||||
return view('produktivitas.petani.lahan.index', compact('petani', 'lahan'));
|
||||
}
|
||||
@@ -39,7 +49,7 @@ class PetaniLahanController extends Controller
|
||||
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate($request->per_page ?? 10);
|
||||
->get();
|
||||
|
||||
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
||||
|
||||
@@ -48,28 +58,98 @@ class PetaniLahanController extends Controller
|
||||
return view('produktivitas.petani.lahan.add', compact('petani', 'lahan', 'provinsi', 'komoditas'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store($petaniId, Request $request)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nama' => ['nullable', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['nullable', 'string', 'max:255'],
|
||||
'profile_id' => ['nullable'],
|
||||
'petani_id' => ['nullable'],
|
||||
'keluarga_pengelola_id' => ['nullable'],
|
||||
'adat_pemilik_id' => ['nullable'],
|
||||
'desa_kelurahan_id' => ['nullable'],
|
||||
'luas_lahan' => ['required', 'numeric'],
|
||||
'path' => ['nullable', 'string'],
|
||||
'komoditas_id' => ['required', 'exists:master_komoditas,id'],
|
||||
'jenis_komoditas_id' => ['required', 'exists:jenis_komoditas,id'],
|
||||
'varietas_id' => ['nullable', 'exists:master_varietas,id'],
|
||||
'jumlah_tanaman' => ['required', 'numeric'],
|
||||
]);
|
||||
|
||||
$komoditas = Komoditas::where('id', $request->komoditas_id)->first();
|
||||
$lahanCount = Lahan::where('petani_id', $petaniId)
|
||||
->count();
|
||||
|
||||
$lahan = Lahan::create([...$validated,
|
||||
'nama' => 'Lahan ' . $komoditas->nama . ' #' . ($lahanCount + 1),
|
||||
'uid' => 'LHN-' . $petani->uid . '-' . strtoupper(Str::random(10)),
|
||||
'petani_id' => $petaniId,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id()]
|
||||
);
|
||||
|
||||
if ($lahan) {
|
||||
$data = collect(range(1, $lahan->jumlah_tanaman))->map(function ($i) use ($lahan, $komoditas) {
|
||||
return [
|
||||
'lahan_id' => $lahan->id,
|
||||
'komoditas_id' => $lahan->komoditas_id,
|
||||
'jenis_komoditas_id' => $lahan->jenis_komoditas_id,
|
||||
'varietas_id' => $lahan->varietas_id,
|
||||
'petani_id' => $lahan->petani_id,
|
||||
'nama' => 'Tanaman ' . $komoditas->nama . ' #' . $i,
|
||||
];
|
||||
});
|
||||
|
||||
$data->chunk(500)->each(function ($chunk) {
|
||||
Tanaman::insert($chunk->toArray());
|
||||
});
|
||||
|
||||
return redirect()->route('petani.lahan-tanaman.index', $petaniId)->with('success', 'Lahan berhasil ditambahkan.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(string $petaniId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||
|
||||
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
||||
|
||||
return view('produktivitas.petani.lahan.view', compact('lahan', 'lahanSelected', 'petani', 'provinsi'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
public function edit(string $petaniId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
$lahan = Lahan::where('petani_id', $petaniId)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$lahanSelected = Lahan::where('petani_id', $petaniId)->findOrFail($id);
|
||||
|
||||
$komoditas = Komoditas::all();
|
||||
|
||||
$provinsi = Provinsi::orderBy('kode', 'asc')->get();
|
||||
|
||||
return view('produktivitas.petani.lahan.edit', compact('lahan', 'lahanSelected', 'petani', 'komoditas', 'provinsi'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user