0

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

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”缩短为有限的字符数量吗?谢谢你。

4

1 回答 1

1

使用访问器和修改器。在您的艺术家模型中:

public function getDescriptionAttribute($value)
{
      // Change 100 to be whatever length you want
      return substr($value, 0, 100);
}
于 2013-07-10T07:50:26.027 回答