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 →
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article
Queue totalSize() and JobInterrupted Event in Laravel 13.31 image

Queue totalSize() and JobInterrupted Event in Laravel 13.31

Read article
Find Unexpected Test Inputs with Fuzz for Pest image

Find Unexpected Test Inputs with Fuzz for Pest

Read article