tutoring/app/Http/Controllers/Admin/LessonController.php

68 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Course;
use App\Models\Lesson;
use App\Models\LessonVideo;
use App\Models\Chapter;
class LessonController extends Controller
{
public function reorder(Request $request)
{
$ids = json_decode($request->order, true);
if (!is_array($ids)) {
return back()->with('error', 'Invalid order data.');
}
foreach ($ids as $index => $id) {
Lesson::where('id', $id)->update(['order' => $index + 1]);
}
return back()->with('success', 'Lesson order updated.');
}
public function show($courseId, $chapterId, Lesson $lesson)
{
// later well validate that the lesson belongs to this chapter & course
return view('admin.lessons.show', compact('lesson', 'chapterId', 'courseId'));
}
public function store(Request $request)
{
$request->validate([
'chapter_id' => 'required|exists:chapters,id',
'title' => 'required|string|max:255',
]);
$order = Lesson::where('chapter_id', $request->chapter_id)->max('order') + 1;
Lesson::create([
'chapter_id' => $request->chapter_id,
'title' => $request->title,
'slug' => Str::slug($request->title),
'description' => $request->description ?? null,
'order' => $order,
]);
return back()->with('success', 'Lesson added.');
}
public function destroy(Course $course, Chapter $chapter, Lesson $lesson)
{
$lesson->delete();
return redirect()->route('admin.chapters.show', [
'course' => $course->slug,
'chapter' => $chapter->slug,
])->with('success', 'Lesson deleted.');
}
}