【问题标题】:How to use str_split() function for printing string character by character如何使用 str_split() 函数逐字符打印字符串
【发布时间】:2020-03-05 05:05:40
【问题描述】:

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

这是我的控制器:

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

【问题讨论】:

    标签: php html css laravel


    【解决方案1】:

    你可以定义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();
    

    【讨论】:

    • 感谢您的回复。我知道我应该使用 mutator,但由于某种原因我不能。但无论如何
    【解决方案2】:

    假设您在将学生传递到刀片文件后尝试将属性打印出来,那么试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多