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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article
Statamic Mailables Viewer Previews Laravel Emails in the Control Panel image

Statamic Mailables Viewer Previews Laravel Emails in the Control Panel

Read article
Laravel Tackle: Run an AI Coding Agent in Your Laravel App image

Laravel Tackle: Run an AI Coding Agent in Your Laravel App

Read article
Queue::forward(): Reroute Laravel Queues in One Place image

Queue::forward(): Reroute Laravel Queues in One Place

Read article
Laravel Read-Through Filesystem: Lazy Storage Migration image

Laravel Read-Through Filesystem: Lazy Storage Migration

Read article