index文件:<td>{{ $student->sex($student->sex) }}</td>
模型文件:Student.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
//通过模型处理性别,先定义三个常量
const SEX_UN = 10; //未知
const SEX_BOY = 20; //男
const SEX_GIRL = 30; //女
protected $table = 'student';
protected $fillable = ['name','age','sex']; //允许批量赋值
public $timestamps = true;
protected function getDateFormat()
{
return time();
}
protected function asDateTime($val)
{
return $val;
}
//定义一个方法sex
public function sex($ind = null)
{
$arr = [
self::SEX_UN => '未知',
self::SEX_BOY => '男',
self::SEX_GIRL => '女',
];
if( $ind !== null ){
return array_key_exists($ind, $arr) ? $arr[$ind] : $arr[self::SEX_UN];
}
return $arr;
}
}
最后index显示报错
ErrorExceptionin helpers.php line 529: htmlentities() expects parameter 1 to be string, array given
in helpers.php line 529
atHandleExceptions->handleError('2', 'htmlentities() expects parameter 1 to be string, array given', 'D:\xampps\htdocs\laravel-demo\vendor\laravel\framework\src\Illuminate\Support\helpers.php', '529', array('value' => array('未知', '男', '女')))
athtmlentities(array('未知', '男', '女'), '3', 'UTF-8', false) in helpers.php line 529
ate(array('未知', '男', '女')) in d5c5164230498b21f0daeb217404370268fd2229.php line 31
atinclude('D:\xampps\htdocs\laravel-demo\storage\framework\views\d5c5164230498b21f0daeb217404370268fd2229.php') in PhpEngine.php line 42
怎么办?
周彪彪