103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\MasterData;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Land;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class LandController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$lands = Land::orderBy('created_at', 'desc')->get();
|
|
return view('master-data.lahan.index', compact('lands'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$customMessage = [
|
|
'name.required' => 'Nama wajib diisi',
|
|
'name.max' => 'Nama maksimal 255 karakter',
|
|
'name.string' => 'Nama harus berupa string',
|
|
|
|
'description.required' => 'Deskripsi wajib diisi',
|
|
'description.max' => 'Deskripsi maksimal 255 karakter',
|
|
'description.string' => 'Deskripsi harus berupa string',
|
|
];
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'land_name' => 'required|string|max:255',
|
|
'description' => 'required|string|max:255',
|
|
], $customMessage);
|
|
|
|
if ($validator->fails()) {
|
|
toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
$land = new Land();
|
|
$land->name = $request->land_name;
|
|
$land->description = $request->description;
|
|
|
|
try {
|
|
$land->save();
|
|
toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
|
|
return redirect()->back();
|
|
} catch (\Throwable $th) {
|
|
toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$customMessage = [
|
|
'name.required' => 'Nama wajib diisi',
|
|
'name.max' => 'Nama maksimal 255 karakter',
|
|
'name.string' => 'Nama harus berupa string',
|
|
|
|
'description.required' => 'Deskripsi wajib diisi',
|
|
'description.max' => 'Deskripsi maksimal 255 karakter',
|
|
'description.string' => 'Deskripsi harus berupa string',
|
|
];
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'land_name' => 'required|string|max:255',
|
|
'description' => 'required|string|max:255',
|
|
], $customMessage);
|
|
|
|
if ($validator->fails()) {
|
|
toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
$land = Land::find($id);
|
|
$land->name = $request->land_name;
|
|
$land->description = $request->description;
|
|
|
|
try {
|
|
$land->save();
|
|
toast('Data berhasil diubah', 'success')->position('top-right')->autoclose(3000);
|
|
return redirect()->back();
|
|
} catch (\Throwable $th) {
|
|
toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$land = Land::find($id);
|
|
try {
|
|
$land->delete();
|
|
toast('Data berhasil dihapus', 'success')->position('top-right')->autoclose(3000);
|
|
return redirect()->back();
|
|
} catch (\Throwable $th) {
|
|
toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
}
|