尝试读取布尔值上的属性"id"。但仅以编辑形式



我想在编辑表单中添加dropdowninput,并从表(类)中添加选项,因此我使用foreach。当我只有输入表单时,它工作得很好,但是当我添加编辑表单时,它给出了一个错误。我不明白为什么。请帮忙,我是Laravel的新手。

这是我输入表单(我也想做一个这样的编辑表单,具有相同的输入和下拉)

这是我的代码。

<!-- INPUT FORM (MODAL) -->
<form method="post" action="{{ url('/grade/create') }}">
@csrf
<div class="row">
<div class="form-group col-md-8 mb-3">
<label>SNum</label>
<input type="text" pattern="M+[0-9]{7,}" minlength="8" maxlength="8" class="form-control" name="SNum"
required="required" />
</div>
<div class="form-group col-md-4 mb-3">
<label>Class</label>
<select class="form-select" name="class[]">
{{-- Works fine --}}
@foreach ($class as $class)
<option value="{{ $class->id }}">{{ $class->class }}</option>
@endforeach
</select>
</div>
</div>
</form>
@foreach ($grades as $grade)
<!-- EDIT FORM (MODAL) -->
<form method="post" action="{{ url('/grade/update' . $grade->id) }}">
@csrf
<div class="row">
<div class="form-group col-md-8 mb-3">
<label>SNum</label>
<input type="text" value="{{ $grade->SNum }}" pattern="M+[0-9]{7,}" minlength="8"
maxlength="8" class="form-control" name="SNum" required="required" />
</div>
<div class="form-group col-md-4 mb-3">
<label>Class</label>
<select class="form-select" name="class[]" id="class{{ $grade->id }}">
@foreach ($class as $class)
{{-- Attempt to read property "id" on bool --}}
<option value="{{ $class->id }}">{{ $class->class }}</option>
@endforeach
</select>
</div>
</div>
</form>
<div class="modal fade" id="deleteModal{{ $grade->id }}" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="post" action="{{ url('/grade/delete/' . $grade->id) }}">
@csrf
<div class="modal-body"
style="height:100px; display:flex; align-items:center; justify-content:center;">
<h5 class="text-center">Are you sure?</h5>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger">Ya</button>
</div>
</form>
</div>
</div>
</div>
@endforeach

控制器

<?php namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsGrade;
use AppModelsGradeClass;
use AppModelsClass;
class MainController extends Controller
{
public function index()
{
$grades = Grade::get();
$class = Class::get();
return view('main', compact('grades', 'class'));
}
public function store(Request $request)
{
$request->validate([
'SNum' => 'required|max:8|min:8',
'class' => 'required'
]);
$grade = Grade::create([
'SNum' => $request['SNum']
]);
if ($request->has('class')) {
foreach ($request['class'] as $classId) {
GradeClass::create([
'grades_id' => $grade->id,
'class_id' => (int) $classId,
]);
}
}
return redirect('/')->with('status', 'Grade added!');
}
public function update(Request $request, Grade $grade)
{
$request->validate([
'SNum' => 'required|max:8|min:8',
'class' => 'required'
]);
$grade->SNum = $request['SNum'];
$grade->save();
GradeClass::where('grades_id', $grade->id)->delete();
if ($request->has('class')) {
foreach ($request['class'] as $classId) {
GradeClass::create([
'grades_id' => $grade->id,
'class_id' => (int) $classId,
]);
}
}
return redirect('/')->with('status', 'Grade updated!');
}
public function destroy(Grade $grade)
{
GradeClass::where('grades_id', $grade->id)->delete();
$grade->delete();
return redirect('/')->with('status', 'Grade deleted!');
}
}

<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersMainController;
Route::get('/', [MainController::class, 'index']);
Route::controller(MainController::class)->prefix('grade')->name('grade.')->group(function () {
Route::post('/create', 'store')->name('store');
Route::post('/update/{grade}', 'update')->name('update');
Route::post('/delete/{grade}', 'destroy')->name('destroy');
});

非常感谢!

试着修复这个问题:

@foreach ($class as $class)

尽管这可以工作,但至少有一次,您已经在循环结束时销毁了原始的$class变量,因此它不再是您开始时的对象数组。

修改代码,使结果数组作为名为$classes的变量传递给blade,这样您就可以使用:

@foreach ($classes as $class)

相关内容

  • 没有找到相关文章

最新更新