Home >Backend Development >PHP Tutorial >Streamline Your Laravel Models with Stringable Attributes

Streamline Your Laravel Models with Stringable Attributes

Robert Michael Kim
Robert Michael KimOriginal
2025-03-05 15:43:23376browse

Streamline Your Laravel Models with Stringable Attributes

Laravel's AsStringable Type converter is a powerful tool that significantly enhances the way you handle string properties in your Eloquent model. By converting string properties to Stringable objects, you can access a large number of string manipulation methods to write cleaner and more expressive code.

This method is especially useful for content-intensive applications where string operations are frequent, helping to keep the controller and view tidy.

use Illuminate\Database\Eloquent\Casts\AsStringable;

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

Here is 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="https://www.php.cn/link/2fc02e925955d516a04e54a633f05608">@</a>')
            ->replace('[[', '<mark>')
            ->replace(']]', '</mark>');
    }

    public function getSeoTitleAttribute()
    {
        return $this->title
            ->title()
            ->limit(60);
    }
}
$article = Article::find(1);
// 直接访问 Stringable 方法
dd($article->title->title());
dd($article->content->words(20));

// 使用计算属性
dd($article->snippet);
dd($article->url_path);
dd($article->formatted_content);

AsStringable

The type converter converts string processing into an elegant, method chained call experience while keeping the code simplicity and maintainability.

The above is the detailed content of Streamline Your Laravel Models with Stringable Attributes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn