Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 3, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 3, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 3, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 3, 2025

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025

      PSA: You don’t need to spend $400+ to upgrade your Xbox Series X|S storage

      June 3, 2025

      UK civil servants saved 24 minutes per day using Microsoft Copilot, saving two weeks each per year according to a new report

      June 3, 2025

      These solid-state fans will revolutionize cooling in our PCs and laptops

      June 3, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Community News: Latest PECL Releases (06.03.2025)

      June 3, 2025
      Recent

      Community News: Latest PECL Releases (06.03.2025)

      June 3, 2025

      A Comprehensive Guide to Azure Firewall

      June 3, 2025

      Test Job Failures Precisely with Laravel’s assertFailedWith Method

      June 3, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025
      Recent

      All the WWE 2K25 locker codes that are currently active

      June 3, 2025

      PSA: You don’t need to spend $400+ to upgrade your Xbox Series X|S storage

      June 3, 2025

      UK civil servants saved 24 minutes per day using Microsoft Copilot, saving two weeks each per year according to a new report

      June 3, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Customizing Model Date Formats in Laravel

    Customizing Model Date Formats in Laravel

    January 6, 2025

    Customizing Model Date Formats in Laravel

    Laravel provides several approaches to control how dates are formatted when models are serialized to arrays or JSON. From global formats to attribute-specific customization, you can ensure consistent date presentation across your application.

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use DateTimeInterface;
    
    class BaseModel extends Model
    {
        protected function serializeDate(DateTimeInterface $date)
        {
            return $date->format('Y-m-d H:i:s');
        }
    }
    

    Let’s explore a practical example of managing different date formats in a booking system:

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentCastsAttribute;
    use DateTimeInterface;
    
    class Booking extends Model
    {
        protected $casts = [
            'check_in' => 'datetime:Y-m-d',
            'check_out' => 'datetime:Y-m-d',
            'created_at' => 'datetime:Y-m-d H:i:s',
        ];
    
        protected function serializeDate(DateTimeInterface $date)
        {
            return $date->format('Y-m-d H:i:s');
        }
    
        protected function checkInFormatted(): Attribute
        {
            return Attribute::make(
                get: fn () => $this->check_in->format('l, F j, Y')
            );
        }
    
        protected function duration(): Attribute
        {
            return Attribute::make(
                get: fn () => $this->check_in->diffInDays($this->check_out)
            );
        }
    
    public function toArray()
        {
            return array_merge(parent::toArray(), [
                'check_in_formatted' => $this->checkInFormatted,
                'duration_nights' => $this->duration,
                'human_readable' => sprintf(
                    '%s for %d nights',
                    $this->check_in->format('M j'),
                    $this->duration
                )
            ]);
        }
    }
    

    Laravel’s date serialization features ensure consistent date formatting throughout your application while providing flexibility for specific use cases.

    Hostinger

    The post Customizing Model Date Formats in Laravel appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWhat Does AI Really Mean?
    Next Article Universal Design for Visual Disabilities in Healthcare – Braille and Large Print – 11

    Related Posts

    Security

    BitoPro Silent on $11.5M Hack: Investigator Uncovers Massive Crypto Theft

    June 3, 2025
    Security

    New Linux Vulnerabilities

    June 3, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Atomfall seems like Fallout at first, but its masterful gameplay is more like Prey

    News & Updates

    PJobRAT Malware Campaign Targeted Taiwanese Users via Fake Chat Apps

    Development

    MIT researchers introduce Boltz-1, a fully open-source model for predicting biomolecular structures

    Artificial Intelligence

    Best practices for maintenance activities in Amazon RDS for Oracle

    Databases
    Hostinger

    Highlights

    sn – sniff out build artifacts

    December 6, 2024

    sn is a tool to help you find build artifacts. It’s also a replacement for…

    MediSecure Data Breach Confirms Impact on Personal and Health Information of Individuals

    May 16, 2024

    Case Study: Travel Next Level

    December 26, 2024

    Farewell to the Fallen: The Cybersecurity Stars We Lost Last Year

    January 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.