@vipin93 your code is a little strange... what exactly is the content of $value when you get this error? Can you try dd($value)
Unexpected data found. Unexpected data found. Trailing data?
I'm try to convert date format using carbon but i'm getting errors
Unexpected data found.
Unexpected data found.
Trailing data
My model
protected $dates = ['date_of_birth'];
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = Carbon::createFromFormat('Y-m-d',$value);
}
my students table
$table->text('student_name');
$table->text('father_name');
$table->text('mother_name');
$table->date('date_of_birth');
<div class="form-group">
<label for="date_of_birth">Date Of Birth (DD/MM/YYYY)</label>
<input class="form-control" name="date_of_birth" id="date_of_birth" value="{{ old('date_of_birth') }}" placeholder="ex-DD/MM/YYYY" required="" data-date-format="dd/mm/yyyy">
</div>
@vipin93 so if you have input date as d/m/Y then you should use that format when creating a Carbon instance, because you are asking Carbon to create FROM format, not to format..
$this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y', $value);
PS. It seems your <input> field is missing a type attribute.
and you will need to validate the date and/or be prepared to catch the error since the probability of the user mis typing is quite high.
@Snapey yes i'm validating it
'date_of_birth' => 'required|date_format:d/m/Y',
@SaeedPrez same errors
InvalidArgumentException in Carbon.php line 582:
The separation symbol could not be found
Unexpected data found.
Trailing data
in Carbon.php line 582
at Carbon::createFromFormat('d/m/Y', object(Carbon)) in Student.php line 24
at Student->setDateOfBirthAttribute(object(Carbon)) in HasAttributes.php line 518
at Model->setAttribute('date_of_birth', object(Carbon)) in Model.php line 1233
at Model->__set('date_of_birth', object(Carbon)) in Student.php line 39
Change $value to $date_of_birth. It's the name indicated in your form.
as saeed said dump $value and check its format. Have a play in tinker with the carbon functions
no
Better dump your data as per advice
@Snapey @SaeedPrez here dd
public function setDateOfBirthAttribute($value)
{
dd($value);
$this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y',$value);
}
and output
"03/12/2001"
ok so the format looks ok now. I'm just wondering if because you have the field listed in the dates array, laravel is not happy about being given a carbon object
if you look at the last stack trace all the function calls are expecting a carbon instance and not a string
@Snapey I was thinking that too but I figured $dates should only apply on data you fetch, not data you're setting...
Edit: Nvm, I was mistaking it with $casts..
docs
When a column is considered a date, you may set its value to a UNIX timestamp, date string (Y-m-d), date-time string, and of course a DateTime / Carbon instance, and the date's value will automatically be correctly stored in your database:
im not sure of the correct fix but i think the current issue is because this field is listed in $dates
@Snapey @SaeedPrez where and what should i change my code?
@Snapey indeed, I believe you found the issue, everything else seems good to me..
InvalidArgumentException in Carbon.php line 582:
The separation symbol could not be found
Unexpected data found.
Trailing data
in Carbon.php line 582
at Carbon::createFromFormat('d/m/Y', object(Carbon)) in Student.php line 25
at Student->setDateOfBirthAttribute(object(Carbon)) in HasAttributes.php line 518
at Model->setAttribute('date_of_birth', object(Carbon)) in Model.php line 1233
at Model->__set('date_of_birth', object(Carbon)) in Student.php line 40
at Student::App\{closure}(object(Student)) in Dispatcher.php line 334
//protected $dates = ['date_of_birth'];
public function setDateOfBirthAttribute($value)
{
//dd($value);
$this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y',$value);
}
@vipin93 hm... try run..
php artisan clear-compiled
php artisan cache:clear
If it still doesn't work, can you show your whole model?
<?php
namespace App;
use Laravel\Scout\Searchable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Student extends Model
{
use Searchable;
protected $fillable = [
'student_name', 'course_id', 'section_id', 'year_of_admission','last_school','reg_no','gender', 'father_name','mother_name', 'date_of_birth', 'status','emer_no', 'father_no', 'father_email','address','city_id','state_id','zip_pin','address1','ccity_id', 'cstate_id','avatar','zip_pin1','transportation','stopage_id','created_course_id'
];
protected $casts = ['date_of_birth'];
public function setDateOfBirthAttribute($value)
{
//dd($value);
$this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y',$value);
}
public static function boot() {
parent::boot();
static::creating(function($model){
foreach ($model->attributes as $key => $value) {
$model->{$key} = empty($value) ? null : $value;
}
});
static::updating(function($model){
foreach ($model->attributes as $key => $value) {
$model->{$key} = empty($value) ? null : $value;
}
});
}
public function getStudentNameAttribute($value)
{
return ucwords($value);
}
public function getFatherNameAttribute($value)
{
return ucwords($value);
}
public function getMotherNameAttribute($value)
{
return ucwords($value);
}
public function city()
{
return $this->belongsTo(City::Class);
}
@vipin93 try comment out the boot() method, see if ti works without it.
@SaeedPrez after commenting boot method its working but i got another problem which i fixed by use of boot() method
QueryException in Connection.php line 647:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column 'stopage_id' at row 1 (SQL: update `students` set `date_of_birth` = 2001-12-03 16:23:34, `stopage_id` = , `transportation` = 2, `updated_at` = 2017-02-11 16:23:34 where `id` = 499)
in Connection.php line 647
at Connection->runQueryCallback('update `students` set `date_of_birth` = ?, `stopage_id` = ?, `transportation` = ?, `updated_at` = ? where `id` = ?', array(object(Carbon), '', '2', '2017-02-11 16:23:34', 499), object(Closure)) in Connection.php line 607
at Connection->run('update `students` set `date_of_birth` = ?, `stopage_id` = ?, `transportation` = ?, `updated_at` = ? where `id` = ?', array(object(Carbon), '', '2', '2017-02-11 16:23:34', 499), object(Closure)) in Connection.php line 477
at Connection->affectingStatement('update `students` set `date_of_birth` = ?, `stopage_id` = ?, `transportation` = ?, `updated_at` = ? where `id` = ?', array(object(Carbon), '', '2', '2017-02-11 16:23:34', 499)) in Connection.php line 416
at Connection->update('update `students` set `date_of_birth` = ?, `stopage_id` = ?, `transportation` = ?, `updated_at` = ? where `id` = ?', array(object(Carbon), '', '2', '2017-02-11 16:23:34', 499)) in Builder.php line 2210
at Builder->update(array('date_of_birth' => object(Carbon), 'stopage_id' => '', 'transportation' => '2', 'updated_at' => '2017-02-11 16:23:34')) in Builder.php line 847
at Builder->update(array('date_of_birth' => object(Carbon), 'stopage_id' => '', 'transportation' => '2', 'updated_at' => '2017-02-11 16:23:34')) in Model.php line 581
at Model->performUpdate(object(Builder)) in Model.php line 501
at Model->save(array()) in Model.php line 448
at Model->update(array('student_name' => 'Miss Vesta Kulas I', 'course_id' => '1', 'section_id' => '5', 'year_of_admission' => '2006', 'last_school' => 'Miss Willow DuBuque V', 'gender' => '2', 'reg_no' => 'GRN2017484428', 'status' => '1', 'father_name' => 'Mr. Dillan Mann', 'mother_name' => 'Joyce Keebler', 'date_of_birth' => '03/12/2001', 'emer_no' => '1135872365', 'father_no' => '7422714995', 'father_email' => '[email protected]', 'address' => '91837 Jast Rue Apt. 560', 'city_id' => '3', 'state_id' => '3', 'zip_pin' => '128016', 'address1' => '76710 Alycia Throughway Suite 216', 'ccity_id' => '4', 'cstate_id' => '3', 'zip_pin1' => '102817', 'transportation' => '2', 'stopage_id' => '')) in StudentsController.php line 120
@vipin93 how about changing your boot() method to only change values that need to be changed..
public static function boot() {
parent::boot();
static::creating(function($model){
foreach ($model->attributes as $key => $value) {
if(empty($model->{$key})) {
$model->{$key} = null;
}
}
});
static::updating(function($model){
foreach ($model->attributes as $key => $value) {
if(empty($model->{$key})) {
$model->{$key} = null;
}
}
});
}
@SaeedPrez yes its working thanks so which answered should i marked as best answered?
Glad I could help ☺ You should mark @snapey answer as best because your initial problem was caused by the `$dat property.
ok so the format looks ok now. I'm just wondering if because you have the field listed in the dates array, laravel is not happy about being given a carbon object
Not sure I deserve that since the original code works fine when I try it, so it really was the boot method causing the issue;
Model;
class Dog extends Model
{
public $dates = ['DOB'];
public function setDOBAttribute($value)
{
$this->attributes['DOB'] = Carbon::createFromFormat('d/m/Y', $value);
}
Tinker;
>>> $d=App\Dog::find(12000)
=> App\Dog {#701
id: 12000,
KnownAs: "Carrickfarm Mika ShCM",
Kennel_id: 0,
Paternal: 47960,
Maternal: 11999,
SiblingSet: null,
ProfileImage: null,
Approved: 1,
Owner_id: null,
Breeder_id: 9515,
Gender: "B",
Colour: "Grizzle and Tan",
DOB: "2008-10-09",
RegistrationType: "",
IdMark: "",
Breed_id: 3,
Variety_id: 1,
Deleted: 0,
Callname: "~kMqn5T4",
Notes: " ",
Country: "GBR",
Image: null,
Image2: null,
PreviousCCs: 0,
}
>>> $d->DOB = '08/10/2008'
=> "08/10/2008"
>>> $d->save();
=> true
>>> $d=App\Dog::find(12000)
=> App\Dog {#695
id: 12000,
KnownAs: "Carrickfarm Mika ShCM",
Kennel_id: 0,
Paternal: 47960,
Maternal: 11999,
SiblingSet: null,
ProfileImage: null,
Approved: 1,
Owner_id: null,
Breeder_id: 9515,
Gender: "B",
Colour: "Grizzle and Tan",
DOB: "2008-10-08",
RegistrationType: "",
IdMark: "",
Breed_id: 3,
Variety_id: 1,
Deleted: 0,
Callname: "~kMqn5T4",
Notes: " ",
Country: "GBR",
Image: null,
Image2: null,
PreviousCCs: 0,
}
>>>
Thanks to all
By the way, those methods creating null fields should not be necessary with 5.4 ?
@Snapey actually that's is not string it's a integer
When working with dates i find this approach the best:
put the date in model variable like this
protected $dates = ['date_of_birth'];
then the setter like this
public function setDateOfBirthAttribute($date)
{
$this->attributes['date_of_birth'] = Carbon::parse($date);
}
If your date support nullable, you can add this for support null.
public function setDateOfBirthAttribute($date)
{
$this->attributes['date_of_birth'] = is_null($date) ? null : Carbon::parse($date);
}
Please or to participate in this conversation.