有一个名为'students'的表,我想将所有数据打印出来,没有任何困难,但是由于某种原因,我必须在表中逐个字符地打印'Name'和'Family'列。我知道我应该使用str_split,但不知道如何使用它。提前thnx。
这是我的控制器:
$students = DB::table('students')->select('*')->get();
假设您在将学生传递到刀片文件之后尝试打印属性,然后尝试以下操作:
// 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>
您可以定义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();