簡體   English   中英

如何使用 str_split() 函數逐字符打印字符串

[英]How to use str_split() function for printing string character by character

有一個名為 'students' 的表,我想打印所有數據,這沒有困難,但由於某種原因,我必須在表中逐字符打印 'Name' 和 'Family' 列。我知道我應該使用 str_split 但不知道如何使用它。 提前謝謝。

這是我的控制器:

  $students = DB::table('students')->select('*')->get();

應該是這樣的

您可以定義mutator來拆分字符:

    protected $appends = ['name', 'family'];

    public function getNameAttribute($value)
    {
        return str_split($value);
    }
    public function getFamilyAttribute($value)
    {
        return str_split($value);
    }

但是,您需要使用 Eloquent-builder 而不是 query-builder:

Student::select('*')->get();

假設您在將學生傳遞到刀片文件后嘗試打印屬性,請嘗試以下操作:

// An example to display the `name` field inside a table, do the same for the family field
<table>
  ...
  @foreach($students as $student)
    ...
    <tr>
      @foreach(str_split($student->name) as $character)
        <td>{{ $character }}</td>
      @endforeach
    </tr>
    ...
  @endforeach
  ...
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM