Laravel 5 how to fill edit form fields with saving database using blade templates

advertisements

I am new to laravel and i am working on a laravel 5 project that requires me to fill a form's fields with their values from database. Here is my controller method:

public function edit($id)
{
    $poem = Poem::where('id', $id)->get();
    return view('admin.poem_edit', compact('poem'));
}

and here is the view (form):

{!! Form::open(array('url' => 'admin/poem', 'class' => 'form')) !!}
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

<div class="form-group">
    {!! Form::label('Title') !!}
    {!! Form::text('title', null, array('required', 'class'=>'form-control')) !!}
</div>

<div class="form-group">
    {!! Form::label('author') !!}
    {!! Form::text('author', null, array('required', 'class'=>'form-control')) !!}
</div>

<div class="form-group">
    {!! Form::label('poem') !!}
    {!! Form::textarea('body', null, array('required', 'class'=>'form-control')) !!}
</div>

<div class="form-group">
    {!! Form::submit('Post New poem', array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}

My goal is to replace the null which represent the form field value into its value as in the database.

I have tried the Input::old('title') method as suggested by some people but it simply didn't work. Thanks for any help


Use Form Model Binding (https://laravelcollective.com/docs/5.2/html#form-model-binding and https://laravel.com/docs/4.2/html#form-model-binding)

Other than that you can change your piece of code to this:

public function edit($id)
{
    return view('admin.poem_edit')->with('poem', Poem::findOrFail($id));
}