update: add filtering data material on teacher
This commit is contained in:
parent
283b9de5bf
commit
3eb4794789
|
|
@ -11,17 +11,17 @@
|
||||||
|
|
||||||
class LiteracyMaterialController extends Controller
|
class LiteracyMaterialController extends Controller
|
||||||
{
|
{
|
||||||
function materials()
|
public function materials()
|
||||||
{
|
{
|
||||||
$materials = LiteracyMaterial::all();
|
$materials = LiteracyMaterial::orderBy('created_at', 'desc')->get();
|
||||||
$users = User::all();
|
$users = User::all();
|
||||||
$questions = LiteracyQuestion::all();
|
$questions = LiteracyQuestion::all();
|
||||||
|
|
||||||
return view('literacy.teacher.materials.index', [
|
return view('literacy.teacher.materials.index', [
|
||||||
'materials' => $materials,
|
'materials' => $materials,
|
||||||
'users' => $users,
|
'users' => $users,
|
||||||
'questions' => $questions,
|
'questions' => $questions,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
|
|
@ -40,22 +40,21 @@ public function store(Request $request)
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'title' => 'required|string|max:255',
|
'title' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048' // Ubah 'file_path' jadi 'file'
|
'file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$filePath = null;
|
$filePath = null;
|
||||||
|
|
||||||
if ($request->hasFile('file')) {
|
if ($request->hasFile('file')) {
|
||||||
// Simpan file dengan nama asli ke folder public/literacy/document
|
|
||||||
$originalName = $request->file('file')->getClientOriginalName();
|
$originalName = $request->file('file')->getClientOriginalName();
|
||||||
$filePath = 'literacy/document/' . $originalName;
|
$path = $request->file('file')->storeAs('public/literacy_materials', $originalName);
|
||||||
$request->file('file')->move(public_path('literacy/document'), $originalName);
|
$filePath = str_replace('public/', '', $path); // Simpan path relatif
|
||||||
}
|
}
|
||||||
|
|
||||||
LiteracyMaterial::create([
|
LiteracyMaterial::create([
|
||||||
'title' => $request->title,
|
'title' => $request->title,
|
||||||
'description' => $request->description,
|
'description' => $request->description,
|
||||||
'file_path' => $filePath, // Simpan path relatif ke folder public
|
'file_path' => $filePath,
|
||||||
'user_id' => auth()->id()
|
'user_id' => auth()->id()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -73,25 +72,20 @@ public function update(Request $request, $id)
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'title' => 'required|string|max:255',
|
'title' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048' // Ubah 'file_path' jadi 'file'
|
'file_path' => 'nullable|file|mimes:pdf,doc,docx|max:2048'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$material = LiteracyMaterial::findOrFail($id);
|
$material = LiteracyMaterial::findOrFail($id);
|
||||||
$filePath = $material->file_path;
|
$filePath = $material->file_path;
|
||||||
|
|
||||||
if ($request->hasFile('file')) {
|
if ($request->hasFile('file')) {
|
||||||
// Hapus file lama jika ada
|
if ($filePath && Storage::exists('public/' . $filePath)) {
|
||||||
if ($filePath) {
|
Storage::delete('public/' . $filePath);
|
||||||
$oldFilePath = public_path($filePath);
|
|
||||||
if (file_exists($oldFilePath)) {
|
|
||||||
unlink($oldFilePath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simpan file baru dengan nama asli ke folder public/literacy/document
|
|
||||||
$originalName = $request->file('file')->getClientOriginalName();
|
$originalName = $request->file('file')->getClientOriginalName();
|
||||||
$filePath = 'literacy/document/' . $originalName;
|
$path = $request->file('file')->storeAs('public/literacy_materials', $originalName);
|
||||||
$request->file('file')->move(public_path('literacy/document'), $originalName);
|
$filePath = str_replace('public/', '', $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
$material->update([
|
$material->update([
|
||||||
|
|
@ -107,12 +101,8 @@ public function destroy($id)
|
||||||
{
|
{
|
||||||
$material = LiteracyMaterial::findOrFail($id);
|
$material = LiteracyMaterial::findOrFail($id);
|
||||||
|
|
||||||
// Hapus file kalau ada
|
if ($material->file_path && Storage::exists('public/' . $material->file_path)) {
|
||||||
if ($material->file_path) {
|
Storage::delete('public/' . $material->file_path);
|
||||||
$filePath = public_path($material->file_path); // Mendapatkan path lengkap di public
|
|
||||||
if (file_exists($filePath)) {
|
|
||||||
unlink($filePath); // Hapus file secara langsung
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$material->delete();
|
$material->delete();
|
||||||
|
|
@ -122,13 +112,12 @@ public function destroy($id)
|
||||||
public function show_materials($id)
|
public function show_materials($id)
|
||||||
{
|
{
|
||||||
$material = LiteracyMaterial::findOrFail($id);
|
$material = LiteracyMaterial::findOrFail($id);
|
||||||
|
$path = storage_path('app/public/' . $material->file_path);
|
||||||
|
|
||||||
// Pastikan file tersedia
|
if (!file_exists($path)) {
|
||||||
if (!$material->file_path || !file_exists(public_path($material->file_path))) {
|
|
||||||
abort(404, 'File tidak ditemukan');
|
abort(404, 'File tidak ditemukan');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arahkan ke file langsung agar bisa ditampilkan di browser
|
return response()->file($path);
|
||||||
return response()->file(public_path($material->file_path));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user