path kategori
This commit is contained in:
parent
88c6d5528c
commit
f0076b5e83
|
|
@ -16,8 +16,8 @@ public function index(Request $request)
|
|||
$kategori = KategoriTps::when($search, function ($query) use ($search) {
|
||||
$query->where('nama_kategori', 'like', '%' . $search . '%');
|
||||
})
|
||||
->orderBy('id_kategori_tps', 'desc')
|
||||
->paginate(10);
|
||||
->orderBy('id_kategori_tps', 'desc')
|
||||
->paginate(10);
|
||||
|
||||
return view('admin.kategori-tps.index', compact('title', 'kategori'));
|
||||
}
|
||||
|
|
@ -48,13 +48,23 @@ public function store(Request $request)
|
|||
]
|
||||
);
|
||||
|
||||
// Upload foto ke public/assets/admin/images/kategori-tps
|
||||
if ($request->hasFile('foto_kategori')) {
|
||||
|
||||
$file = $request->file('foto_kategori');
|
||||
|
||||
$filename = strtolower(str_replace(' ', '_', $request->nama_kategori))
|
||||
. '_' . time()
|
||||
. '.' . $file->getClientOriginalExtension();
|
||||
$file->move(public_path('assets/admin/images/kategori-tps'), $filename);
|
||||
|
||||
$path = public_path('assets/admin/images/kategori-tps');
|
||||
|
||||
// buat folder otomatis jika belum ada
|
||||
if (!file_exists($path)) {
|
||||
mkdir($path, 0777, true);
|
||||
}
|
||||
|
||||
$file->move($path, $filename);
|
||||
|
||||
$data['foto_kategori'] = $filename;
|
||||
}
|
||||
|
||||
|
|
@ -69,6 +79,7 @@ public function edit($id)
|
|||
{
|
||||
$title = 'Edit Kategori TPS';
|
||||
$kategori = KategoriTps::findOrFail($id);
|
||||
|
||||
return view('admin.kategori-tps.edit', compact('title', 'kategori'));
|
||||
}
|
||||
|
||||
|
|
@ -93,17 +104,26 @@ public function update(Request $request, $id)
|
|||
]
|
||||
);
|
||||
|
||||
$path = public_path('assets/admin/images/kategori-tps');
|
||||
|
||||
if (!file_exists($path)) {
|
||||
mkdir($path, 0777, true);
|
||||
}
|
||||
|
||||
if ($request->hasFile('foto_kategori')) {
|
||||
// Hapus foto lama jika ada
|
||||
if ($kategori->foto_kategori && file_exists(public_path('assets/admin/images/kategori-tps/' . $kategori->foto_kategori))) {
|
||||
unlink(public_path('assets/admin/images/kategori-tps/' . $kategori->foto_kategori));
|
||||
|
||||
if ($kategori->foto_kategori && file_exists($path . '/' . $kategori->foto_kategori)) {
|
||||
unlink($path . '/' . $kategori->foto_kategori);
|
||||
}
|
||||
|
||||
$file = $request->file('foto_kategori');
|
||||
|
||||
$filename = strtolower(str_replace(' ', '_', $request->nama_kategori))
|
||||
. '_' . time()
|
||||
. '.' . $file->getClientOriginalExtension();
|
||||
$file->move(public_path('assets/admin/images/kategori-tps'), $filename);
|
||||
|
||||
$file->move($path, $filename);
|
||||
|
||||
$data['foto_kategori'] = $filename;
|
||||
}
|
||||
|
||||
|
|
@ -118,8 +138,10 @@ public function destroy($id)
|
|||
{
|
||||
$kategori = KategoriTps::findOrFail($id);
|
||||
|
||||
if ($kategori->foto_kategori && file_exists(public_path('assets/admin/images/kategori-tps/' . $kategori->foto_kategori))) {
|
||||
unlink(public_path('assets/admin/images/kategori-tps/' . $kategori->foto_kategori));
|
||||
$path = public_path('assets/admin/images/kategori-tps');
|
||||
|
||||
if ($kategori->foto_kategori && file_exists($path . '/' . $kategori->foto_kategori)) {
|
||||
unlink($path . '/' . $kategori->foto_kategori);
|
||||
}
|
||||
|
||||
$kategori->delete();
|
||||
|
|
|
|||
Loading…
Reference in New Issue