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

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

这是我的控制器:

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

http://img2.mukewang.com/63491d9e0001834c04850116.jpg

梦里花落0921
浏览 108回答 2
2回答

慕田峪7331174

假设您在将学生传递到刀片文件后尝试打印属性,请尝试以下操作:// An example to display the `name` field inside a table, do the same for the family field<table>&nbsp; ...&nbsp; @foreach($students as $student)&nbsp; &nbsp; ...&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; @foreach(str_split($student->name) as $character)&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $character }}</td>&nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </tr>&nbsp; &nbsp; ...&nbsp; @endforeach&nbsp; ...</table>

白猪掌柜的

您可以定义mutator来拆分字符:&nbsp; &nbsp; protected $appends = ['name', 'family'];&nbsp; &nbsp; public function getNameAttribute($value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return str_split($value);&nbsp; &nbsp; }&nbsp; &nbsp; public function getFamilyAttribute($value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return str_split($value);&nbsp; &nbsp; }但是,您需要使用 Eloquent-builder 而不是 query-builder:Student::select('*')->get();
打开App,查看更多内容
随时随地看视频慕课网APP