Laravel error : Call to a member function format() on string
up vote
-1
down vote
favorite
i'm using Laravel 5.6 and i want to make an accessor in my Utility model like this
public function getRekomtekDateAttribute($value)
{
return $value->format('d-m-Y');
}
but when i call {{ $utility->rekomtek_date }} the error, as in the title, shown
i've added this line in the same model, as in Laravel error: Call to a member function format() on string , but still no luck
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'rekomtek_date'
];
i don't know what's wrong. since i was using Laravel 5.3 this always occur -_-'
laravel
add a comment |
up vote
-1
down vote
favorite
i'm using Laravel 5.6 and i want to make an accessor in my Utility model like this
public function getRekomtekDateAttribute($value)
{
return $value->format('d-m-Y');
}
but when i call {{ $utility->rekomtek_date }} the error, as in the title, shown
i've added this line in the same model, as in Laravel error: Call to a member function format() on string , but still no luck
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'rekomtek_date'
];
i don't know what's wrong. since i was using Laravel 5.3 this always occur -_-'
laravel
is yourrekomtek_dateof DATE type in the DB? Try dumping out the$valuebefore formatting and make sure that it is a Carbon type.
– nakov
Nov 9 at 23:54
yep, i'm using$table->date('rekomtek_date');in my migration
– Bang Fady
Nov 18 at 16:23
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
i'm using Laravel 5.6 and i want to make an accessor in my Utility model like this
public function getRekomtekDateAttribute($value)
{
return $value->format('d-m-Y');
}
but when i call {{ $utility->rekomtek_date }} the error, as in the title, shown
i've added this line in the same model, as in Laravel error: Call to a member function format() on string , but still no luck
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'rekomtek_date'
];
i don't know what's wrong. since i was using Laravel 5.3 this always occur -_-'
laravel
i'm using Laravel 5.6 and i want to make an accessor in my Utility model like this
public function getRekomtekDateAttribute($value)
{
return $value->format('d-m-Y');
}
but when i call {{ $utility->rekomtek_date }} the error, as in the title, shown
i've added this line in the same model, as in Laravel error: Call to a member function format() on string , but still no luck
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'rekomtek_date'
];
i don't know what's wrong. since i was using Laravel 5.3 this always occur -_-'
laravel
laravel
asked Nov 9 at 23:30
Bang Fady
51110
51110
is yourrekomtek_dateof DATE type in the DB? Try dumping out the$valuebefore formatting and make sure that it is a Carbon type.
– nakov
Nov 9 at 23:54
yep, i'm using$table->date('rekomtek_date');in my migration
– Bang Fady
Nov 18 at 16:23
add a comment |
is yourrekomtek_dateof DATE type in the DB? Try dumping out the$valuebefore formatting and make sure that it is a Carbon type.
– nakov
Nov 9 at 23:54
yep, i'm using$table->date('rekomtek_date');in my migration
– Bang Fady
Nov 18 at 16:23
is your
rekomtek_date of DATE type in the DB? Try dumping out the $value before formatting and make sure that it is a Carbon type.– nakov
Nov 9 at 23:54
is your
rekomtek_date of DATE type in the DB? Try dumping out the $value before formatting and make sure that it is a Carbon type.– nakov
Nov 9 at 23:54
yep, i'm using
$table->date('rekomtek_date'); in my migration– Bang Fady
Nov 18 at 16:23
yep, i'm using
$table->date('rekomtek_date'); in my migration– Bang Fady
Nov 18 at 16:23
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You should not pass a $value to the getter function use it like this:
public function getRekomtekDateAttribute()
{
return $this->rekomtek_date->format('d-m-Y');
}
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the$valueis empty or null, but that actually it is of type String, and string does not have a format method.
– nakov
Nov 18 at 19:58
add a comment |
up vote
0
down vote
Thats because you are trying to use format() in a string.
You should do:
use CarbonCarbon;
...
public function getRekomtekDateAttribute($value)
{
return Carbon::parse($value)->format('d-m-Y');
}
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You should not pass a $value to the getter function use it like this:
public function getRekomtekDateAttribute()
{
return $this->rekomtek_date->format('d-m-Y');
}
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the$valueis empty or null, but that actually it is of type String, and string does not have a format method.
– nakov
Nov 18 at 19:58
add a comment |
up vote
0
down vote
accepted
You should not pass a $value to the getter function use it like this:
public function getRekomtekDateAttribute()
{
return $this->rekomtek_date->format('d-m-Y');
}
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the$valueis empty or null, but that actually it is of type String, and string does not have a format method.
– nakov
Nov 18 at 19:58
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You should not pass a $value to the getter function use it like this:
public function getRekomtekDateAttribute()
{
return $this->rekomtek_date->format('d-m-Y');
}
You should not pass a $value to the getter function use it like this:
public function getRekomtekDateAttribute()
{
return $this->rekomtek_date->format('d-m-Y');
}
answered Nov 10 at 0:15
nakov
1,663188
1,663188
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the$valueis empty or null, but that actually it is of type String, and string does not have a format method.
– nakov
Nov 18 at 19:58
add a comment |
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the$valueis empty or null, but that actually it is of type String, and string does not have a format method.
– nakov
Nov 18 at 19:58
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
but why in here at the Defining An Accessor part, it says that we define accessor with that $value variable
– Bang Fady
Nov 18 at 16:21
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the
$value is empty or null, but that actually it is of type String, and string does not have a format method.– nakov
Nov 18 at 19:58
@BangFady Yes, it works well on a string type, but for a date, the conversion to Carbon happens when you access the parent of the field. So your error does not says that the
$value is empty or null, but that actually it is of type String, and string does not have a format method.– nakov
Nov 18 at 19:58
add a comment |
up vote
0
down vote
Thats because you are trying to use format() in a string.
You should do:
use CarbonCarbon;
...
public function getRekomtekDateAttribute($value)
{
return Carbon::parse($value)->format('d-m-Y');
}
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
add a comment |
up vote
0
down vote
Thats because you are trying to use format() in a string.
You should do:
use CarbonCarbon;
...
public function getRekomtekDateAttribute($value)
{
return Carbon::parse($value)->format('d-m-Y');
}
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
add a comment |
up vote
0
down vote
up vote
0
down vote
Thats because you are trying to use format() in a string.
You should do:
use CarbonCarbon;
...
public function getRekomtekDateAttribute($value)
{
return Carbon::parse($value)->format('d-m-Y');
}
Thats because you are trying to use format() in a string.
You should do:
use CarbonCarbon;
...
public function getRekomtekDateAttribute($value)
{
return Carbon::parse($value)->format('d-m-Y');
}
edited Nov 9 at 23:58
answered Nov 9 at 23:54
Erick Patrick
30124
30124
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
add a comment |
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
You can read a bit in the docs first: laravel.com/docs/5.7/eloquent-mutators#date-mutators adding the field in a $dates array it casts it to a Carbon instance. Something else is wrong, this is not a solution.
– nakov
Nov 9 at 23:57
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
@navok Yep, BUT the conversion to a Carbon instance takes effect when trying to access the object. E.g. $user->my_date will return a Carbon instance. But when retrieved inside the accessor, it's not a Carbon instance yet.
– Erick Patrick
Nov 10 at 0:08
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
His usage is wrong, look at my answer. It works 100%. I've been using it so many times.
– nakov
Nov 10 at 0:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234564%2flaravel-error-call-to-a-member-function-format-on-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
is your
rekomtek_dateof DATE type in the DB? Try dumping out the$valuebefore formatting and make sure that it is a Carbon type.– nakov
Nov 9 at 23:54
yep, i'm using
$table->date('rekomtek_date');in my migration– Bang Fady
Nov 18 at 16:23