Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何查看特定课程的主题_Php_Laravel - Fatal编程技术网

Php 如何查看特定课程的主题

Php 如何查看特定课程的主题,php,laravel,Php,Laravel,我有一个课程列表,每门课程都有一个按钮,允许我查看该特定课程的所有科目 首先,我使用了这个命令,以便laravel已经知道父元素是什么: php artisan make:controller CourseSubjectController --model=Subject --parent=Course 在course index.php文件中,我有一个按钮,可以查看该特定课程的所有主题: <td><a class="btn btn-success" href="{{rout

我有一个课程列表,每门课程都有一个按钮,允许我查看该特定课程的所有科目

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

php artisan make:controller CourseSubjectController --model=Subject --parent=Course
在course index.php文件中,我有一个按钮,可以查看该特定课程的所有主题:

<td><a class="btn btn-success" href="{{route('departments.course.subjects.index', [$course->department->id, $course->id])}}">Subjects</a></td>
当我访问链接时,我遇到以下错误:

"Type error: Argument 1 passed to App\Http\Controllers\CourseSubjectController::index() must be an instance of App\Course, string given"
我如何查看课程的主题


提前感谢

它会抛出一个错误,因为您没有将课程实例传递到路线。我假设您正在使用GET请求,并且在您的路线中,您只传递课程部门id及其id:

[$course->department->id, $course->id]
因此,也许您可以在控制器中执行此操作:

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

你说得对@Pol。我应该在index函数中再传递一个参数。它现在起作用了,我感谢大家的支持
public function index($departmentId, $courseId)
{
    $course = Course::findOrFail($courseId);
    return $course->subjects;
}