169 lines
5.0 KiB
PHP
169 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
use DataTables;
|
|
use GuzzleHttp\Client;
|
|
use Carbon\Carbon;
|
|
|
|
use App\Models\Akun;
|
|
|
|
class AkunController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Akun::select('*');
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('title-post', function($row){
|
|
$text = '
|
|
<p class="mb-0">' . $row->gejala . '</p>
|
|
';
|
|
return $text;
|
|
})
|
|
->rawColumns(['title-post'])
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Akun',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('akun.create'),
|
|
'name' => 'Tambah Baru'
|
|
]
|
|
];
|
|
|
|
return view('admin.app.content.akun.index', compact('data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Tambah baru',
|
|
];
|
|
|
|
return view('admin.app.content.akun.add', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_akun' => 'required',
|
|
'kode_akun' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first());
|
|
}
|
|
|
|
$input = $request->all();
|
|
|
|
$post = new Akun([
|
|
'nama_akun' => $input['nama_akun'], // Membersihkan input judul menggunakan Purifier
|
|
'kode_akun' => $input['kode_akun'], // Membersihkan input deskripsi menggunakan Purifier
|
|
]);
|
|
|
|
$check = Akun::where('kode_akun', $input['kode_akun'])->where('nama_akun', $input['nama_akun'])->count();
|
|
if ($check == 0) {
|
|
if ($post->save()) {
|
|
return redirect()->route('akun')->with('success', 'You have successfully added data');
|
|
} else {
|
|
return redirect()->route('akun')->with('error', 'An error occurred in the query');
|
|
}
|
|
} else {
|
|
return redirect()->route('akun')->with('error', 'Title already exists');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Edit: ' . Akun::where('id', $id)->first()->nama_akun,
|
|
];
|
|
|
|
$akun = Akun::FindOrFail($id);
|
|
|
|
return view('admin.app.content.akun.edit', compact('data', 'id', 'akun'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
// Validasi input sebelum memperbarui data
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_akun' => 'required',
|
|
'kode_akun' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first());
|
|
}
|
|
|
|
// Cari data berdasarkan ID
|
|
$akun = Akun::find($id);
|
|
|
|
// Jika data ditemukan
|
|
if ($akun) {
|
|
|
|
// Update data dengan data baru dari form yang telah dibersihkan
|
|
$akun->nama_akun = $request->input('nama_akun');
|
|
$akun->kode_akun = $request->input('kode_akun');
|
|
// Simpan perubahan pada database
|
|
$akun->save();
|
|
return redirect()->route('akun')->with('success', 'You are successfully added new records');
|
|
} else {
|
|
return redirect()->route('akun')->with('error', 'Unexpected error');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
// Cari data berdasarkan ID
|
|
$post = Akun::find($id);
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Hapus data dari database
|
|
$post->delete();
|
|
return redirect()->route('akun')->with('success', 'You are successfully deleted records');
|
|
} else {
|
|
return redirect()->route('akun')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
} |