截断Laravel 4中数据库中的文本

时间:2013-07-10 07:46:45

标签: model laravel laravel-4 substring

我有一个模型用于从查询中提取数据库中的一些数据。我试图缩短存储在其中一列(“描述”)中的文本。这是模型函数:

public static function fan_likes() {
        $fan_likes = DB::table('fanartists')
                    ->join('artists', 'fanartists.artist_id', '=', 'artists.id')
                    ->where('fanartists.fan_id', '=', Auth::user()->id)
                    ->select('artists.id', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
                    ->get();


        return $fan_likes;

        }

你知道如何在提取这些数据时将“artists.description”缩短到有限的字符数量吗?谢谢。

1 个答案:

答案 0 :(得分:1)

Use Accessors and Mutators.在您的艺术家模型中:

public function getDescriptionAttribute($value)
{
      // Change 100 to be whatever length you want
      return substr($value, 0, 100);
}