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 →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article