[英]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 .有一个名为 'students' 的表,我想打印所有数据,这没有困难,但由于某种原因,我必须在表中逐字符打印 'Name' 和 'Family' 列。我知道我应该使用 str_split 但不知道如何使用它。 thnx in advance.
提前谢谢。
this is my controller:这是我的控制器:
$students = DB::table('students')->select('*')->get();
you can defined mutator to split the characters:您可以定义mutator来拆分字符:
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:但是,您需要使用 Eloquent-builder 而不是 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.