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

      7 MagSafe accessories that I recommend every iPhone user should have

      June 1, 2025

      I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

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

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025
      Recent

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025

      Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 22/2025

      June 1, 2025

      Rilasciata PorteuX 2.1: Novità e Approfondimenti sulla Distribuzione GNU/Linux Portatile Basata su Slackware

      June 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»String Manipulation Made Easy with Laravel’s AsStringable Cast

    String Manipulation Made Easy with Laravel’s AsStringable Cast

    January 8, 2025

    String Manipulation Made Easy with Laravel's AsStringable Cast

    Laravel’s AsStringable cast transforms model attributes into Stringable objects, providing fluent access to Laravel’s powerful string manipulation methods directly from your model attributes.

    When working with text in your Laravel models, you often need to perform various string operations like formatting, cleaning, or transforming the text. While you could do this manually each time, Laravel’s AsStringable cast transforms your model’s string attributes into powerful Stringable objects, giving you access to dozens of built-in string manipulation methods.

    This cast is particularly useful when you consistently need to perform string operations on specific model attributes. Instead of writing the same string manipulation code repeatedly, you can access these operations directly on your model attributes as if they were Stringable objects.

    use IlluminateDatabaseEloquentCastsAsStringable;
    
    class Post extends Model
    {
        protected $casts = [
            'title' => AsStringable::class,
            'content' => AsStringable::class,
        ];
    }
    

    Let’s explore a practical example of a blog system with SEO-friendly content handling:

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentCastsAsStringable;
    
    class Article extends Model
    {
        protected $casts = [
            'title' => AsStringable::class,
            'content' => AsStringable::class,
            'meta_description' => AsStringable::class
        ];
    
        public function getSlugAttribute()
        {
            return $this->title
                ->lower()
                ->replaceMatches('/[^a-z0-9s]/', '')
                ->replace(' ', '-')
                ->limit(60, '');
        }
    
        public function getSeoTitleAttribute()
        {
            return $this->title
                ->title()
                ->limit(60, '...')
                ->append(' | My Blog');
        }
    
        public function getExcerptAttribute()
        {
            return $this->content
                ->stripTags()
                ->words(50, '...')
                ->title();
        }
    
        public function getReadingTimeAttribute()
        {
            return $this->content
                ->stripTags()
                ->wordCount() / 200;
        }
    }
    

    The AsStringable cast simplifies string manipulation by providing a fluent interface for common text operations, making your code more readable and maintainable.


    The post String Manipulation Made Easy with Laravel’s AsStringable Cast 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 ArticleRecurr is a PHP library for working with recurrence rules for calendar dates
    Next Article Immutable Value Objects in PHP and Laravel With the Bags Package

    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

    CVE-2025-4943 – LA-Studio Element Kit for Elementor Stored Cross-Site Scripting Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    A Coding Tutorial of Model Context Protocol Focusing on Semantic Chunking, Dynamic Token Management, and Context Relevance Scoring for Efficient LLM Interactions

    Machine Learning

    highlight.js – syntax highlighter

    Linux

    Crafting AEP Schemas: Practical Guide

    Development

    Highlights

    Playwright E2E Testing: A Fast & Practical Introduction

    July 10, 2024

    Comments Source: Read More 

    Cloud Data Management: QA’s Master Key  

    December 5, 2024

    The Future of Work: Letting AI Handle Responsibility While Humans Maintain Accountability

    April 25, 2025

    FBI Seizes BreachForums Again, Urges Users to Report Criminal Activity

    May 15, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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