Laravel更新了如何更新索引中的数据

时间:2018-06-14 07:22:37

标签: php laravel eloquent

问题是我想跳过该编辑页面。它工作得很好。但我想在索引视图中编辑我的数据。

我试过这个,但我接受了这个错误

{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
  <input class="form-control" value="@foreach ($choices as $choice){{ $choice->question_number }}@endforeach" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
  

尝试获取非对象的属性(查看:C:\ wamp64 \ www \ zainsurgalt \ resources \ views \ choices \ index.blade.php)

索引视图

<td><a href="{{ route('choices.edit', $duplicate->topic->id) }}" class="btn btn-default">Edit</a></td>

修改视图

{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
    <input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}

控制器更新

public function update(Request $request,Choice $choice){
     Choice::where('id', $choice->id)->update([
        'question_number' => $request->input('number')
        ]);
  return redirect()->route('choices.index');
}

1 个答案:

答案 0 :(得分:1)

所有当你获得对象属性时,你必须检查对象是否存在

@if (!empty($duplicate->topic))
    <td><a href="{{ route('choices.edit', $duplicate->topic->id) }}" class="btn btn-default">Edit</a></td>
@endIf

@if (!empty($choice))
    {!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
        <input class="form-control" type="number" name="number"></input>
    {!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
    {!! Form::close() !!}
@endIf

最后你使用

@foreach ($choices as $choice){{ $choice->question_number }}@endforeach

$choice更改为其他名称,例如$_choice,以免混淆上面使用的$choice