Reputation: 679
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...
Upvotes: 1
Views: 2741
Reputation: 8124
What I did is replaced the Form::input('date', 'published_at', ...
line related segment with the following code section (referring to your Laracast Partials and Forms Reuse tutorial) specifically:
<!-- Published_at Form Text Input -->
<div class="form-group">
{!! Form::label('published_at', 'Publish On:') !!}
{!! Form::date('published_at', (isset($article) && $article->published_at ? $article->published_at : date('Y-m-d')), ['class' => 'form-control']) !!}
</div>
In the above replaced code section (isset($article) && $article->published_at ? $article->published_at : date('Y-m-d'))
would check if the Form has given an $article
object, so then use its existing published_at
date or other wise create date with current time and use it.
Hope this would be helpful to somebody out there.
Upvotes: 1
Reputation: 95
Clearly this question is closed already, but maybe it can help someone. I was using a form to both edit and create, so I did the following:
<div class="form-group">
<label class="control-label" for="date_">Proyect Date </label>
<input class="form-control" id="datepicker" name="date" placeholder="DD/MM/AA" value="
@if (isset($proyect->date))
{!!date('d-m-Y', strtotime($proyect->date))!!}
@endif" type="text">
Upvotes: 0
Reputation: 679
On the offchance anyone else has a similar issue - I've now found the solution, which was embarrassingly simple!
I changed the date field as below:
{!! Form::date('first_published', $article->first_published, ['class' => 'nameofclasshere']) !!}
So that the field's default value is the value pulled from my database (which I was able to access elsewhere in the page as detailed in the question). When I submit the form the date remains as the value from the database, unless I change the form value, in which case it's updated.
Don't know if this is the correct or recommended way to do this (and is still different to how I've done the other fields), but it seems to do the trick!
Upvotes: 1