Laravel Tutorials

Streamline Your Laravel Models with Stringable Attributes

Published
Streamline Your Laravel Models with Stringable Attributes image

Laravel's AsStringable cast is a powerful tool that can significantly enhance how you work with string attributes in your Eloquent models. By transforming your string attributes into Stringable objects, you gain access to a wide array of string manipulation methods, leading to cleaner and more expressive code.

This approach is particularly valuable for content-heavy applications where string manipulation is frequent, helping you maintain cleaner controllers and views.

use Illuminate\Database\Eloquent\Casts\AsStringable;

class Post extends Model
{
    protected function casts(): array
    {
        return [
            'title' => AsStringable::class,
            'content' => AsStringable::class
        ];
    }
}

Here's a practical example of a content management system:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\AsStringable;

class Article extends Model
{
    protected function casts(): array
    {
        return [
            'title' => AsStringable::class,
            'content' => AsStringable::class,
            'meta_description' => AsStringable::class
        ];
    }

    public function getSnippetAttribute()
    {
        return $this->content
            ->stripTags()
            ->words(30, '...');
    }

    public function getUrlPathAttribute()
    {
        return $this->title
            ->slug()
            ->prepend('/articles/');
    }

    public function getFormattedContentAttribute()
    {
        return $this->content
            ->markdown()
            ->replaceMatches('/\@mention\((.*?)\)/', '<a href="/users/$1">@$1</a>')
            ->replace('[[', '<mark>')
            ->replace(']]', '</mark>');
    }

    public function getSeoTitleAttribute()
    {
        return $this->title
            ->title()
            ->limit(60);
    }
}
$article = Article::find(1);
// Access Stringable methods directly
dd($article->title->title());
dd($article->content->words(20));

// Use computed attributes
dd($article->snippet);
dd($article->url_path);
dd($article->formatted_content);

The AsStringable cast transforms string handling into an elegant, method-chaining experience while keeping your code clean and maintainable.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article
Real-Time Inertia Props With Laravel Reverb image

Real-Time Inertia Props With Laravel Reverb

Read article
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article