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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 31, 2025

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

      May 31, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 31, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 31, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025

      How to install SteamOS on ROG Ally and Legion Go Windows gaming handhelds

      May 31, 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

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025
      Recent

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025

      Filament Is Now Running Natively on Mobile

      May 31, 2025

      How Remix is shaking things up

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

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025
      Recent

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Accessing Raw Model Data with Laravel’s attributesToArray Method

    Accessing Raw Model Data with Laravel’s attributesToArray Method

    December 31, 2024

    Accessing Raw Model Data with Laravel's attributesToArray Method

    When working with Eloquent models, sometimes you need just the core database attributes without relationships or computed properties. Laravel’s attributesToArray method provides a clean way to access this raw model data.

    // Basic usage
    $user = User::first();
    $attributes = $user->attributesToArray();
    // Returns raw database attributes
    // ['id' => 1, 'name' => 'John', 'email' => 'john@example.com']
    

    Let’s explore a practical example implementing an audit system for model changes:

    <?php
    
    namespace AppModels;
    
    use AppModelsAuditLog;
    use IlluminateDatabaseEloquentModel;
    
    class AuditableModel extends Model
    {
        protected static function booted()
        {
            static::updated(function ($model) {
                $original = $model->getOriginal();
                $current = $model->attributesToArray();
                
                // Compare only actual database attributes
                $changes = array_diff($current, $original);
                
                if (!empty($changes)) {
                    AuditLog::create([
                        'model_type' => get_class($model),
                        'model_id' => $model->id,
                        'original' => json_encode($original),
                        'changes' => json_encode($changes),
                        'user_id' => auth()->id(),
                        'timestamp' => now()
                    ]);
                }
            });
        }
    }
    
    class Product extends AuditableModel
    {
        protected $appends = ['formatted_price', 'stock_status'];
        
        public function category()
        {
            return $this->belongsTo(Category::class);
        }
        
        public function getFormattedPriceAttribute()
        {
            return "$" . number_format($this->price / 100, 2);
        }
    }
    

    The attributesToArray method provides direct access to model attributes as stored in the database, making it perfect for scenarios where you need the raw data without additional computed properties or relationships.


    The post Accessing Raw Model Data with Laravel’s attributesToArray Method 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 ArticleCodeSOD: A Set of Mistakes
    Next Article Laravel News 2024 Recap

    Related Posts

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    May 31, 2025
    Artificial Intelligence

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

    May 31, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Dubbi nello scegliere la giusta immagine Debian? Ecco finalmente una guida chiara!

    Linux

    CodeSOD: While This Works

    Development

    Age of Mythology: Retold Immortal Pillars expansion arrives in March, bringing Chinese myth to Xbox and PC

    News & Updates

    How to Build a REST API in Django

    Development

    Highlights

    Sustainability Week at Figma

    February 1, 2025

    As part of our first-ever Sustainability Week, we’re sharing an update on our climate investments—and…

    Per Metacritic, Elden Ring: Shadow of the Erdtree is now the best-reviewed DLC of all time, topping The Witcher 3: Blood and Wine

    June 19, 2024

    ASUS VivoBook S 15 Copilot+ PC Review: Is the Snapdragon X Elite hype real?

    June 18, 2024

    tere – terminal file explorer

    December 28, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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