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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

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

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

      June 1, 2025

      New Xbox games launching this week, from June 2 through June 8 — Zenless Zone Zero finally comes to Xbox

      June 1, 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

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

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

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025
      Recent

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

      June 1, 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.


    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 

    Hostinger
    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

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 1, 2025
    Artificial Intelligence

    LWiAI Podcast #201 – GPT 4.5, Sonnet 3.7, Grok 3, Phi 4

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Why Conversational AI Chatbots Are a Game-Changer for Your Business

    Development

    ONLYOFFICE 8.3 Released with Support for Apple iWork Files

    Linux

    Bluesky gets a TikTok mode, but Vine’s resurection might steal its thunder

    Operating Systems

    CVE-2025-4790 – FreeFloat FTP Server GLOB Command Handler Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)
    GetResponse

    Highlights

    VPBank Builds OpenAPI Platform With MongoDB

    April 28, 2025

    Open banking is the practice of banks sharing some of their financial data and services…

    How to get LinkedIn Premium for free

    June 28, 2024
    Parallels Desktop 20.3 Brings Linux VM Fixes to Mac Users

    Parallels Desktop 20.3 Brings Linux VM Fixes to Mac Users

    April 19, 2025

    Former Polish Deputy Minister Charged with Misuse of Funds to Buy Pegasus Spyware

    August 30, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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