Laravel form form binding - date formatting

I have Laravel. I have a form. I have a MySQL database. It has several dates. When I link the model and the form, the form is filled with raw MySQL dates. This is obviously not what I need.

My question is: how do I get the form associated with the model to display dates in a more readable way?!? Unable to intercept and format data. Or maybe for a more general solution, is there a way to do some preliminary action on any data between the model and the form before the user sees it?

I think all this is wrong? Thank you so much!

+7
forms binding laravel laravel-4 model
source share
3 answers

You can add accessor in your model as follows:

public function getCreatedAtAttribute($date) { $date = new \Carbon\Carbon($date); // Now modify and return the date } 

This will be called for created_at . Also check date-mutators if you need to override the default values. Check Carbon Documents .

+6
source share

You can format the date on the form using the format() method.

If you use it in a form element:

 {{ Form::text('name', $model->created_at->format('d/m/YH:i')) }} 

If you show it as plain text:

 {{ $model->created_at->format('d/m/YH:i') }} 

If you want the user to be able to change the date correctly, you can use three different selection fields. Here is an excerpt from a project I was working on that allows the user to change the date of birth:

 <div class="form-group {{ $errors->has('date_of_birth') ? 'has-error' : '' }}"> {{ Form::label('date_of_birth', 'Date of Birth', ['class' => 'control-label']) }} <div class="form-inline"> {{ Form::selectRange('date_of_birth[day]', 1, 31, null, ['class' => 'form-control']) }} {{ Form::selectMonth('date_of_birth[month]', null, ['class' => 'form-control']) }} {{ Form::selectYear('date_of_birth[year]', date('Y') - 3, date('Y') - 16, null, ['class' => 'form-control']) }} </div> {{ $errors->first('date_of_birth', '<span class="help-block">:message</span>') }} </div> 

It is contained in a form associated with it.

Side note

The alternative answer posted here actually overrides the default method that laravel handles with dates, and unless you actually return a carbon object, you will never have the luxury of using the format method or any other convenient thing that carbon has.

If you have other columns containing dates, add them to the list of data mutators:

 public function getDates() { return ['created_at', 'updated_at', 'date_of_birth', 'some_date_column']; } 

Now it will make each of them an instance of Carbon , allowing you to format as you like, anytime and easily modify, duplicate, and a number of other things. For more information about this see: http://laravel.com/docs/eloquent#date-mutators

+2
source share

You can format all fields retrieved from the database with the creation of Accessor in your model. For example, if the database field created_at used, getCreatedAtAttribute used.

 public function getCreatedAtAttribute($date) { return Carbon::($date)->format('d/m/Y'); } 
0
source share

All Articles