127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Jalan;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Cviebrock\EloquentSluggable\Services\SlugService;
|
|
|
|
class JalanController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$data = Jalan::all();
|
|
// dd($data);
|
|
return view('dashboard.jalan.index', ['data' => $data]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('dashboard.jalan.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->validate($request, [
|
|
'nama_jalan' => 'required',
|
|
// 'latitude' => 'required',
|
|
// 'longitude' => 'required',
|
|
]);
|
|
|
|
//insert to table
|
|
$data = $request->all();
|
|
// $data['slug'] = Slugservice::createslug(Jalan::class, 'slug', $request->nama_jalan);
|
|
Jalan::create($data);
|
|
|
|
// dd ($data);
|
|
if ($data) {
|
|
return redirect()
|
|
->route('jalan.index')
|
|
->with([
|
|
'success' => 'Data Jalan Berhasil Ditambahkan',
|
|
]);
|
|
} else {
|
|
return redirect()
|
|
->back()
|
|
->withInput()
|
|
->with([
|
|
'error' => 'Some problem occurred, please try again',
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function edit($slug)
|
|
{
|
|
$data = DB::table('jalans')
|
|
->where('slug', $slug)
|
|
->first();
|
|
|
|
return view('dashboard.jalan.edit', ['data' => $data]);
|
|
// return view('dashboard.jalan.edit');
|
|
}
|
|
|
|
public function update(Request $request, Jalan $jalan)
|
|
{
|
|
$this->validate($request, [
|
|
'nama_jalan' => 'required',
|
|
// 'latitude' => 'required',
|
|
// 'longitude' => 'required',
|
|
]);
|
|
|
|
//insert to table
|
|
$data = $request->all();
|
|
$data['slug'] = Slugservice::createslug(Jalan::class, 'slug', $request->nama_jalan);
|
|
$jalan->update($data);
|
|
// dd($jalan);
|
|
|
|
// dd ($data);
|
|
if ($data) {
|
|
return redirect()
|
|
->route('jalan.index')
|
|
->with([
|
|
'success' => 'Data Jalan Berhasil Diubah',
|
|
]);
|
|
} else {
|
|
return redirect()
|
|
->back()
|
|
->withInput()
|
|
->with([
|
|
'error' => 'Some problem has occured, please try again',
|
|
]);
|
|
}
|
|
// return redirect('/jalan')->with('data Berhasil Di Update');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
DB::table('jalans')->where('id', $id)->delete();
|
|
|
|
// alihkan halaman ke halaman jalan dengan pesan sukses
|
|
return redirect()
|
|
->back()
|
|
->with([
|
|
'success' => 'Data Jalan Berhasil Dihapus',
|
|
]);
|
|
} catch (\Illuminate\Database\QueryException $e) {
|
|
if ($e->getCode() == '23000') {
|
|
// Integrity constraint violation
|
|
return redirect()
|
|
->back()
|
|
->with([
|
|
'error' => 'Data Jalan tidak dapat dihapus karena terkait dengan data kecelakaan.',
|
|
]);
|
|
}
|
|
|
|
// Tangani pengecualian lainnya
|
|
return redirect()
|
|
->back()
|
|
->with([
|
|
'warning' => 'Terjadi kesalahan saat menghapus data jalan.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
}
|