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

问题描述 投票:0回答:2

有一个名为'students'的表,我想将所有数据打印出来,没有任何困难,但是由于某种原因,我必须在表中逐个字符地打印'Name'和'Family'列。我知道我应该使用str_split,但不知道如何使用它。提前thnx。

这是我的控制器:

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

should be like this

php html css laravel
2个回答
1
投票

假设您在将学生传递到刀片文件之后尝试打印属性,然后尝试以下操作:

// 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>

2
投票

您可以定义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();
© www.soinside.com 2019 - 2024. All rights reserved.