Kyle Wardle
Kyle Wardle

Reputation: 830

Carbon\Carbon not found inconsistently - Laravel 5.5

So currently I pull back a date and convert it to a readable format in carbon:

</div>
<div class="">
 <label for="Name">Letter Sent:</label>
 @if (is_null($Client->letter_posted))
 @else
   {{  \carbon\carbon::createFromFormat('Y-m-d',$Client->letter_posted)->format('d/m/Y')}}
 @endif
</div>

And it works when testing (Testing both whilst letter_posted is null and not) however very occasionally it will spit out the error on the live server:

Class 'carbon\carbon' not found

It has only occurred 3 times in the last 2 months very randomly and a refresh of the page will remove this error eg. the error appears, if you refresh the page it is no longer there.

Any help appreciated.

Upvotes: 4

Views: 3039

Answers (2)

Laerte
Laerte

Reputation: 7083

Change the code to:

{{  \Carbon\Carbon::createFromFormat('Y-m-d',$Client->letter_posted)->format('d/m/Y')}}

This happens when you deploy the system in a case sensitive server.

Upvotes: 7

dbrekelmans
dbrekelmans

Reputation: 1051

Most likely you are receiving the error because you're using carbon\carbon somewhere in your code instead of \carbon\carbon.

Putting the \ in front refers to the global namespace. Without the \, you are referring to a class that might not exist (which is the error you are getting).

See: Class 'App\Carbon\Carbon' not found Laravel 5


You can create an alias to avoid using the full name in Laravel. In app.php, go to aliases and add 'Carbon' => 'Carbon\Carbon'. You can then use it like this: {{ Carbon::createFromFormat('Y-m-d', $Client->letter_posted)->format('d/m/Y') }}


Additional note: while PHP namespaces are not case-sensitive, it is good practice to treat them as case-sensitive: use \Carbon\Carbon instead of \carbon\carbon.

Upvotes: 1

Related Questions