1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

laravelで表示する文字を制限したい(はみ出た分は「...」にする。)

Posted at

概要

記事のタイトルに、表示する上での文字数制限を付けたい。
ここでは

.php
  $post->title

に対して、表示する文字数を制限する。
こんな漢字

「昨今の少子高齢化問題について」 → 「昨今の少子高齢化...」

モデル側

Post.php
  public function strlength() {
          $str = $this->title;
          $limit = 10;

          if (mb_strlen($str) > $limit ) {
              $str = mb_substr($str, 0, $limit);
              return $str . "...";
          } else {
              return $str;
          }
  }

ビュー側

indexblade.php
:
:
    {{ $post->strlength() }}
:
:
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?