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