【问题标题】:Laravel update how to update data in indexLaravel更新如何更新索引中的数据
【发布时间】:2018-06-14 07:22:36
【问题描述】:

问题是我想跳过那个编辑页面。它工作正常。但我想在索引视图中编辑我的数据。

我试过了,但我犯了这个错误

{!! 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');
}

【问题讨论】:

  • 当你点击编辑时使用一个模态来代替显示
  • 哦,好主意。我忘记了模态。你能举出什么好例子吗?
  • 为什么在模态中应用 foreach 值
  • 我想你已经在{{ $choice-&gt;question_number }}...中得到了那个question_number ...
  • 你检查 $choice, $duplicate->topic 是否为空??

标签: php laravel eloquent


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2016-01-19
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2022-11-02
    相关资源
    最近更新 更多