【问题标题】:Laravel model form binding not working for editLaravel 模型表单绑定不适用于编辑
【发布时间】:2017-08-29 09:19:56
【问题描述】:

我有一个控制器TourCategoryController.php 并且有编辑方法:

public function edit(TCategory $tCategory)
{
    return view('admin.manage.tour.category.edit')->withCategory($tCategory);
}

下面是我认为edit的代码:

<div class="col-sm-4">
    {{Form::model($category,['route' => ['tour-category.update', $category->id ], 'method' => "PUT"]) }}
    <input type="text" class="form-control" id="name" name="name">
    <label for="name">Name</label>
    {{ Form::close() }}
</div>

我遇到的问题是,输入字段没有被表单模式绑定填充。

检查编辑表单动作属性时显示action="http://localhost:8000/manage/tour-category",而它应该类似于action="http://localhost:8000/manage/tour-category/{id}"

控制器的路由:

Route::prefix('manage')
->middleware('role:superadministrator|administrator|user')
->group(function () {
         Route::resource('tour-category','TourCategoryController');
});

【问题讨论】:

  • 也向我们展示您的路线!
  • 使用 laravel 表单进行表单模型绑定,laravel foem 模型绑定在 html 表单中不起作用
  • 尝试使用 HTML 集合进行模型绑定。同样的问题。

标签: php laravel laravel-5.4


【解决方案1】:

使用 laravel 文本字段代替纯文本字段。

 {{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}

【讨论】:

    【解决方案2】:

    如果你没有使用Form Facades

    <div class="col-sm-4">
        <form method="POST" action="{{ route('tour-category.update', $category->id) }}">
            {{ method_field('PUT') }}
            {{ csrf_field() }}
    
            <label for="name">Name</label>
            <input type="text" id="name" name="name" class="form-control">
        </form>
    </div>
    

    【讨论】:

      【解决方案3】:

      使用

      {{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
      

      而不是

       <input type="text" class="form-control" id="name" name="name">
      

      【讨论】:

        猜你喜欢
        • 2016-03-30
        • 1970-01-01
        • 2013-05-28
        • 2017-03-14
        • 2015-12-17
        • 2017-06-27
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多