samedi 23 avril 2016

Laravel 5.2 show saved date in edit form

This is probably a really, really simple issue, but it's got me stumped...

So, I've got an article saved in the database with a form, including a field to save a published date. The data entered in this field is successfully saving in the database, along with the rest of the data.

I'm successfully pulling all the saved data back out into the edit form, EXCEPT the published_date field.

I've tried using the same format as the other fields, eg title, both show below

{!! Form::text('title', null) !!}
{!! Form::input('date', 'first_published', null) !!} 

This puts dd/mm/yy in the input box, rather than the saved date. If this value isn't changed, the date is saved in the database as today's date.

I've also tried removing the 'date' attribute

  {!! Form::input('first_published', null ) !!}

This results in a totally empty box (and no datepicker), which doesn't overwrite the value in the form if not saved. Better, but still not what I want, as I want to show the saved date, which can be changed if required.

I've also tried echoing the $article->published_date in various ways in this field, but only end up with the same results as those above. I can echo out this data elsewhere in the form no problem though using {!! $article->first_published !!}

The form references the model as so:

{!! Form::model($article, ['method' => 'PATCH', 'url' => 'articles'.'/'. $article->id]) !!}

And the relevant controller functions are

   //page containing form to edit article, pulls in existing data
public function edit($id)
{
    $article = article::findOrFail($id);

    return view('articles.edit', compact('article'));
}

//responsible for updating the article record
public function update($id, Request $request)
{
    $article = article::findOrFail($id);
    $article->update($request->all());

    return redirect('articles');
}

The field is set as a timestamp in the migration, and in the model I have

protected $dates = [
    'first_published'
];

So, if anyone can shed any light on how to get the saved data value into the form field as in the other fields, it would be much appreciated! :) I'm pretty new to laravel so apologies if there's some blindingly obvious issue I've missed...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire