【问题标题】:Truncating text from a database in Laravel 4在 Laravel 4 中从数据库中截断文本
【发布时间】:2013-07-08 01:19:27
【问题描述】:

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

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

【问题讨论】:

    标签: model laravel laravel-4 substring


    【解决方案1】:

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

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

    【讨论】:

    • 我将如何实现这一点?在视图中调用这个?或者在模型查询中使用这个函数。
    • @user1072337 只要你想通过 $model 获取它->description 它将被截断。
    猜你喜欢
    • 2019-08-04
    • 2012-07-23
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多