So you want to save the date without the time, in Y-m-d format? What does your table migration look like?
Date saves to database as 0000-00-00 00:00:00
Hello there, I hope someone can help. Ive been researching for sometime now on how to properly save from a form to the database when a user choose a specific date. On the create page it looks as such:
create.blade.php
{!! Form::label('published_date', 'Published Date', ['class' => 'control-label']) !!}
{!! Form::input('date', 'published_date', date('Y-m-d'), ['class' => 'form-control']) !!}
Controller (Store)
{
$binding = Binding::with('id');
$categories = Categories::with('id');
$conditions = Condition::with('id');
$editions = Edition::with('id');
$publishers = Publisher::with('id');
$rarities = Rarity::with('id');
$signatures = Signature::with('id');
// Availability option switch (on = 1; off = 0)
$available = Input::all();
// Convert added_date format to be compatible with database ISO 8601 format
$added = Input::all();
$added['added_date'] = date('Y-m-d', strtotime($added['added_date']));
// Convert published_date format to be compatible with database ISO 8601 format
$published = Input::all();
$published['published_date'] = date('Y-m-d', strtotime($published['published_date']));
$book = new Book($request->all());
Auth::user()->books()->
save(
$book,
$binding,
$categories,
$conditions,
$editions,
$publishers,
$rarities,
$signatures,
$added,
$published,
$available);
return redirect('admin/library');
Controller (Show)
public function show($id)
{
$books = DB::table('books')->where('books.id', '=', $id)
->join('bindings', 'books.binding_id', '=', 'bindings.id')
->join('categories', 'books.category_id', '=', 'categories.id')
->join('conditions', 'books.condition_id', '=', 'conditions.id')
->join('editions', 'books.edition_id', '=', 'editions.id')
->join('publishers', 'books.publisher_id', '=', 'publishers.id')
->join('rarities', 'books.rarity_id', '=', 'rarities.id')
->join('signatures', 'books.signature_id', '=', 'signatures.id')
->join('users', 'books.user_id', '=', 'users.id')
->first();
return view('admin.library.show')
->with('books', $books);
}
Model
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Book extends Model {
/**
* @var string
*/
protected $primaryKey = 'id';
/**
* Allowed Fields for mass Input
*
* @var array
*/
protected $fillable = [
'title',
'author',
'sec_author',
'thi_author',
'for_author',
'availability',
'barcode',
'isbn',
'dewey',
'universal',
'loc',
'added_date',
'binding_id',
'category_id',
'condition_id',
'edition_id',
'publisher_id',
'rarity_id',
'signature_id'
];
/**
* @var array
*/
protected $dates = ['added_date', 'published_date'];
/**
* @return array
*/
public function getDates()
{
// Return any columns that are date or datetime types
// to automatically have Laravel convert them to Carbon
// objects. (Also include created_at, updated_at and
// deleted_at if you use them)
return ['added_date', 'published_date'];
}
/**
* @param $date
*/
public function setAddedDateAttribute($date){
$this->attributes['added_date'] = Carbon::createFromFormat('Y-m-d', $date);
}
/**
* @param $date
*/
public function setPublishedDateAttribute($date){
$this->attributes['published_date'] = Carbon::toFormattedDateString('Y-m-d', $date);
}
I have tried a bunch of different methods from what I have read and saw, but honestly I cant believe it can be this complex for a user to simply choose from a datetime picker in the form and save it. My frustration level is high :( A big thank you to everyone in advance.
By setting the $dates in your model it calls Carbon to create the time stamp already. so technically a set attribute isn't required.
In your View if your input is type date if formats the date already for you.
To return the proper format for display in the input type date (as it will not show by default)
public function getPublishedAtAttribute($published_at)
{
return $this->attributes['publish_at'] = Carbon::parse($publish_at)->format('Y-m-d');
}
Also notice the $publish_at not $date. Eloquent has no idea what $date is unless you have a date column in your table.
Please or to participate in this conversation.