简体   繁体   中英

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

there is a table named 'students' and I want to print all datas out, there is no difficulty in it, but for some reason I have to print 'Name' and 'Family' columns character by character in table.I know I should use str_split but don't know how to use it . thnx in advance.

this is my controller:

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

应该是这样的

you can defined mutator to split the characters:

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

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

However, you need to use Eloquent-builder instead of query-builder:

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

Assuming you are trying to print the attributes out after passing students into a blade file then try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM