use carbon function in laravel view(Blade template)

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Use Carbon Functions in Laravel Views (Blade Templates) Body: Laravel provides great support for working with dates through the Carbon library, which is a PHP date and time utility that allows you to manipulate dates easily. In your Laravel application, you might have data from the database, like customer timestamps, and want to convert them to human-readable formats or perform calculations using Carbon functions in the view file (Blade template). This tutorial aims at helping you achieve this task effectively by providing a correct and thorough answer. 1. Firstly, ensure that your Laravel project is set up correctly with Composer and your dependencies installed. Use the command "composer install" to verify if all required packages have been installed. 2. Including Carbon in Your Project: You need to make sure that you have the Carbon library included in your project. Run the command "composer require laravel/carbon" to add it as a dependency, or simply import it manually with a 'use \Carbon\Carbon;' statement. 3. Passing Data From Controller to View: You need first to pass the customer data from your controller to the view using the 'return view('customer-list', ['customers' => $customers]);' syntax, where 'customers' refers to the customer array. 4. Using Carbon Functions in the View (Blade) File: Once you have passed the customer data to the view, you can use Carbon functions on these values. However, make sure that your Blade file imports the necessary namespace for using the Carbon library. Append 'use \Carbon\Carbon;' to the top of your Blade file or import it from the main app.blade.php. 5. Code Example with Carbon Functions: Following the given example where you want to calculate the difference in days between a customer creation and update timestamps, use the following code: foreach($customer as $time){ $create = \Carbon\Carbon::now()->setTimestamp(strtotime($time->created_at)); $update = \Carbon\Carbon::now()->setTimestamp(strtotime($time->updated_at)); $create_at_difference = \Carbon\Carbon::parse($create)->diff(\Carbon\Carbon::now())->days; } This code will convert the timestamps to Carbon objects, then calculate and display the difference in days between them. You can also use other functions such as addDays(), subDays(), diffForHumans(), and more to manipulate and format your date values however you need. 6. Final Steps: Ensure that your code is well-organized, and all dependencies are included. Test the functionality of your Laravel application to verify if it meets your requirements and works as expected. For further assistance or support on using Carbon functions in Laravel views, check out our resources at https://laravelcompany.com/blog/manipulating-dates-with-carbon-in-laravel/. In conclusion, this comprehensive guide has provided a clear answer to using the Carbon function within Laravel views (Blade templates) while offering best practices and relevant code examples. By following these steps, you can easily integrate date manipulation and formatting into your Laravel application for a more efficient user experience.