I get some values from the database and I passed those values into view from the controller. now I want to use that data with some carbon function
in Laravel view.
In my View file I wrote
foreach($customer as $time){
$create= $time->created_at;
$update= $time->updated_at;
$create_at_difference=\Carbon\Carbon::createFromTimestamp(strtotime($create))->diff(\Carbon\Carbon::now())->days;
}
when I try like this it returns "Class 'Carbon' not found"
How can I do this?
It works with global namespace to my view.blade.php as
{{ \Carbon\Carbon::parse($row->posted_at)->diffForHumans() }}
If you want to use the namespaced class, you don't need the first slash:
$create_at_difference=Carbon\Carbon::createFromTimestamp(strtotime($create))->diff(\Carbon\Carbon::now())->days;
You should just write Carbon\Carbon instead of \Carbon\Carbon.
That is a quick solution. But, using classes directly in your views is a bad idea. You can add more functionality to your model by creating a function that will return current created at difference.
lets say you have Customer model, you can go that way:
use Carbon\Carbon;
class Customer extends Eloquent
{
public function created_at_difference()
{
return Carbon::createFromTimestamp(strtotime($this->created_at))->diff(Carbon::now())->days;
}
}
then in the view you can access this like:
@foreach($customers as $customer)
{{$customer->created_at_difference()}}
@endforeach
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With