我有包含课程的课程,每节课程都有测试。点击课程时,我正在尝试显示测试。
我已经创建了模型,控制器和视图,但似乎不起作用。
这是课程的模型
public function course()
{
return $this->belongsTo(Course::class, 'course_id')->withTrashed();
}
public function test() {
return $this->hasOne('App\Test');
}
这是控制器
public function show($id)
{
$course = Course::with( 'lessons')->with('activeLessons')->findOrFail($id);
$created_bies = \App\User::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = \App\User::get()->pluck('name', 'id');
// $test = \App\Test::where('course_id', $id)->get();
$lesson = \App\Lesson::where('course_id', $id)->get();
// $course_test = Course::with('tests')->findOrFail($id);
$user = User::find(1);
$user->name;
return view('admin.courses.showCourse', compact('course', 'test', 'lesson','course_test', 'previous_lesson', 'next_lesson','date', 'user'));
}
function view_tests($id)
{
$lessons = Lesson::findOrFail($id);
$lessons->test;
return view('admin.courses.test', compact('lessons'));
这是路线
Route::get('/test/{id}', 'EmployeeCoursesController@view_tests')->name('test.show');
这是Blade,其中包含显示测试的链接
@foreach($course->activeLessons as $lesson)
<article class="lesson" >
<p></p>
<p></p>
{!! $loop->iteration!!}. <a href="{{route('learn.show', $lesson->id)}}"></a>
<div class="body" id="title"> {!!$loop->iteration!!}. <h4><a href="{{route('test.show', $lesson->id)}}">{{ $lesson->title }}</a></div>
<p> {!! $lesson->short_description !!}</p>
<iframe width="420" height="315" src="{{ $lesson->video_link}}" frameborder="0" allowfullscreen></iframe>
</article>
@endforeach
答案 0 :(得分:0)
问题出在测试刀片上。该代码运行良好。