laravel - Call to a member function format() on a non-object when dealing with date/time -



laravel - Call to a member function format() on a non-object when dealing with date/time -

i'm trying save info database , need format date/time before save.

my code follows:

public static function tosqldate($date, $formatresult = true) { if (!$date) { homecoming null; } $datetime = datetime::createfromformat(default_datetime_format, $date, new datetimezone(default_timezone)); homecoming $formatresult ? $datetime->format('y-m-d') : $datetime; }

when seek save next error in debugger. homecoming line line highlighted having error.

symfony \ component \ debug \ exception \ fatalerrorexception phone call fellow member function format() on non-object

i'm not sure how effort prepare this. there way can output each of values beingness pulled in before throws error? i'm trying figure out $datetime not beingness returned object.

any help appreciated.

some additional information:

i have created utility/alias pointing the function above , in model repository access via following.

public function save($publicid, $data) { ... $form->date = utils::tosqldate($data['date']); ... $form->save(); ... }

and have next field in view

{{ former::text('date')->addclass('form-control form-control-inline input-medium default-date-picker')->data_bind("datepicker: date, valueupdate: 'afterkeydown'") }}

you may create mutator method in eloquent model create conversion automatic when save model, example, if have timestamp dob in user model add together next method in user model:

public function setdobattribute($value) { $dt = \carbon\carbon::createfromformat('y/m/d', $value)->todatestring(); $this->attributes['dob'] = $dt; }

so can save user model easilt, like:

$user = new user; $user->dob = input::get('dob'); // ... $user->save();

the date automatically set using mutator/setdobattribute method defined in user model, don't need anything.

laravel laravel-4

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -