使用laravelcollective形成模型绑定不在laravel 5.2中工作

时间:2016-07-08 14:58:29

标签: laravel

你好我是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

2 个答案:

答案 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() !!}