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 -_-'










share|improve this question






















  • 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















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 -_-'










share|improve this question






















  • 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













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 -_-'










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 23:30









Bang Fady

51110




51110












  • 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


















  • 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
















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












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');
}





share|improve this answer





















  • 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


















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');
}





share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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');
}





share|improve this answer





















  • 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















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');
}





share|improve this answer





















  • 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













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');
}





share|improve this answer












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');
}






share|improve this answer












share|improve this answer



share|improve this answer










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 $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


















  • 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
















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












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');
}





share|improve this answer























  • 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















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');
}





share|improve this answer























  • 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













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');
}





share|improve this answer














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');
}






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Landwehr

Reims

Javascript gets undefined on array