gpt4 book ai didi

php - Laravel - undefined variable : modules

转载 作者:行者123 更新时间:2023-12-03 18:41:20 27 4
gpt4 key购买 nike

我正在尝试创建一个新模块 - 返回以下错误:

Undefined variable: modules (View: /Users/kerrymckinney/Desktop/LearningHub/resources/views/admin/module/index.blade.php)

索引.blade.php;

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<p>

<a href="{{ route('admin.modules.create') }}"><button type="button" class="btn btn-success">Create Module</button></a>

</p>
<div class="card">
<div class="card-header">Modules</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Module Title</th>
<th>Course Title</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($modules as $module)
<tr>
<th scope="row">{{ $module->id }}</th>
<td>{{ $module->title }}</td>
<td>{{ $module->course->title ?? ''}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection

模块 Controller ;

<?php

namespace App\Http\Controllers\Admin;

use App\User;
use App\Role;
use App\Course;
use App\Module;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ModulesController extends Controller
{
public function index(Request $request)
{
$modules = new module();

if ($request->input('course_id')) {
$modules = $modules->where('course_id', $request->input('course_id'));
} $modules = $modules->get(); //passing modules with course_id through

//$modules = $modules->all();
return view('admin.module.index', compact('modules'));
}

public function create()
{
$courses = Course::all()->pluck('title', 'id');

return view('admin.module.create', compact('courses'));
}

public function store(Request $request)
{
$module = Module::create($request->all());

return view('admin.module.index', ['course_id' => $request->id]); //redirects to correct route by adding course_id in parameter

}

}

我不确定我做错了什么,有人可以帮忙吗?我是 Laravel 的新手。

谢谢。

最佳答案

试试这个

 public function index(Request $request)
{
if ($request->input('course_id')) {
$modules = Module::where('course_id', $request->input('course_id'))->get();
}else{
$modules = Module::all();
}


return view('admin.module.index', compact('modules'));
}

关于php - Laravel - undefined variable : modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60617829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com