如何修复Laravel 5.2中的“Undefined variable:subtask”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复Laravel 5.2中的“Undefined variable:subtask”相关的知识,希望对你有一定的参考价值。
需要编辑我的子任务表记录,SubtaskController
public function edit($projectId,$taskId,$subtaskId)
{
$subtask = Subtask::where('project_id', $projectId)
->where('task_id', $taskId)
->where('id', $subtaskId)
->first();
return view('subtasks.edit')->withTask($subtask)->with('projectId', $projectId)->with('id',$subtaskId);
//
刀片文件链接到编辑按钮就是这个,tasks / index.blade.php
@foreach ($task->subtasks as $subtask)
<ul>
<li>
<div>
<div class="pull-right icons-align">
<a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/subtasks/{{$subtask->id}}/edit" class="editInline"><i class="glyphicon glyphicon-pencil"></i></a>
<a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/delete" class="editInline" onclick="return confirm('Are you sure to want to delete this record?')"><i class="glyphicon glyphicon-trash"></i></a>
</div>
{{ $subtask->subtask_name }}
</div>
@endforeach
}
和路线是这个
Route::get('projects/{projects}/tasks/{tasks}/subtasks/{subtasks}/edit', [
'uses' => 'AppHttpControllersSubtasksController@edit',
]);
和edit.blade.php subtasks / edit.blade.php
<div class="col-lg-6">
<form class="form-vertical" role="form" method="post" action="{{ url('projects/' .$projectId .'/tasks/' . $task->id.'/subtasks/'.$subtask->id) }}"> //this is line 9
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{!! $subtask->subtask_name ?: '' !!}">
@if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Update Subtask</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!! method_field('PUT') !!}
</form>
但是当我从刀片文件中单击编辑按钮得到此错误按摩时,
ErrorException in 9b021095a9def87aed61575bb51efc07798e08da.php line 9: Undefined variable: subtask (View: C:UsersdnkDesktopddd
esourcesviewssubtasksedit.blade.php)
怎么能解决这个问题?
答案
代替:
->withTask($subtask)
做这个:
->withSubtask($subtask)
并在edit
视图中更改此:
$task->id
至:
$subtask->task->id
要使第二个修复工作,您还需要在Subtask
模型中具有正确定义的关系:
public function task()
{
return $this->belongsTo(Task::class);
}
以上是关于如何修复Laravel 5.2中的“Undefined variable:subtask”的主要内容,如果未能解决你的问题,请参考以下文章
如何修复 laravel 5.2 zizaco 委托:迁移类名验证?