如何查看特定课程的科目

时间:2018-05-28 18:19:56

标签: php laravel

我有一份课程清单,每门课程都有一个课程,可以让我查看该课程的所有科目。

首先,我使用了这个命令,因此laravel已经知道父元素是什么:

php artisan make:controller CourseSubjectController --model=Subject --parent=Course

在index.php文件中我有一个按钮,可以让我查看该特定的所有主题:

<td><a class="btn btn-success" href="{{route('departments.course.subjects.index', [$course->department->id, $course->id])}}">Subjects</a></td>

显然网址是正确的:

控制器如下所示:

public function index(Course $course)
{
    return $course->subjects;
}

当我访问链接时遇到此错误:

"Type error: Argument 1 passed to App\Http\Controllers\CourseSubjectController::index() must be an instance of App\Course, string given"

如何查看课程科目?

提前致谢

1 个答案:

答案 0 :(得分:1)

It throws an error because you didn't pass a Course instance to your route. I assume you are using GET request and in your route you are only passing the course department id and its id :

[$course->department->id, $course->id]

So maybe you could do this in your controller instead :

public function index($departmentId, $courseId)
{
    $course = Course::findOrFail($courseId);
    return $course->subjects;
}