mleontenko
5 months ago
339
3
Laravel

LaravelCollective form model binding

Posted 5 months ago by mleontenko

I have a form for editing posts that looks like this:

{!! Form::model($post, ['action' => ['[email protected]', $post->id], 'method' => 'POST']) !!}       
        <div class="form-group">
            {{Form::label('title', 'Title:')}}
            {{Form::text('title', '', ['id' => 'title', 'class' => 'form-control'])}}
        </div> 
	<div class="form-group">
            {{Form::label('content', 'Contenti:')}}
            {{Form::textarea('content', '', ['id' => 'content', 'class' => 'form-control'])}}
        </div>      
        {{Form::hidden('_method', 'PUT')}}   
        {{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
    {!! Form::close() !!}

The $post model looks like this:

{
   "id":1,
   "title":"Post one",
   "content":"Lorem ipsum dolor sit amet...",
}

Is there a way to automatically fill form with content of $post model (which is passed when opening form) without me explicitly setting values to form like this:

{{Form::text('title', $post->title, ['id' => 'title', 'class' => 'form-control'])}}

Form::text requires second parameter to be some value, so I cannot leave it empty....

Please sign in or create an account to participate in this conversation.