Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 435
Kyle Wardle Avatar asked Jan 30 '23 02:01

Kyle Wardle


2 Answers

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.

like image 143
Laerte Avatar answered Jan 31 '23 20:01

Laerte


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.

like image 33
dbrekelmans Avatar answered Jan 31 '23 20:01

dbrekelmans