你好我是laravel
5.2的新手并经历了一些课程。
出于某种原因,表单模型绑定对我不起作用。
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController@update', $post->id]]) !!}
我在$post
收到了数据,因为我正在使用这样的解决方法:
{!! Form::text('title', "$post->title" ,['class'=> 'form-control']) !!}
这显示了我的数据。
控制器:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller{
public function update(Request $request, $id){
$post =Post::findOrfail($id);
$post->update($request->all());
return redirect('/posts');
}
}
create.blade.php view:
@section('content')
<h1>Create Post</h1>
{!! Form::open(['method'=>'POST', 'action'=>'PostController@store']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit('Create Post', ['class'=> 'btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
@endsection
答案 0 :(得分:4)
您不应该在输入中引用null
的值:
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
应该是
{!! Form::text('title', null, ['class'=> 'form-control']) !!}
答案 1 :(得分:0)
好的,尝试将表单的主体添加到名为posts/partials/form.blade.php
的部分中,并将其包含在表单open / model和form close标签之间。
示例:
posts/partials/form.blade.php
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit($formButtonText, ['class'=> 'btn-primary form-control']) !!}
</div>
posts/create.blade.php
{!! Form::open(['method'=>'POST', 'action'=>'PostController@store']) !!}
@include('posts.partials.form', [
'formSubmitButtonText' => 'Create Post'
])
{!! Form::close() !!}
posts/edit.blade.php
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController@update', $post->id]]) !!}
@include('posts.partials.form', [
'formSubmitButtonText' => 'Update Post'
])
{!! Form::close() !!}