隐藏来自 Laravel 的填充模型
Hide filled in model from Laravel
我想用 Laravel 创建一个序列化程序。目前,我有我的模型 (CountryEntity
),具有 getSingleCountry()
隐藏特定字段的功能 (is_active
)。
型号
class CountryEntity extends Model
{
public $table = "countries";
protected $fillable = ['id', 'name', 'code', 'language', 'is_active'];
public $timestamps = false;
public function getSingleCountry()
{
$this->makeHidden['is_active'];
return $this;
}
}
控制器
public function show($id)
{
$country = $this->country_repository->getById($id);
$country = $country->getSingleCountry();
return Response::json(['type' => 'success', 'message' => 'Get country',
'data' => $country, 'status' => 200], 200);
}
但是字段 "is_active" 始终可见...
此处的问题在方法调用中缺失 ()
。这一行:
$this->makeHidden['is_active'];
没有明确地做任何事情。有点令人惊讶的是,这并没有引发 Undefined index
错误,但无论如何。
尝试调用Class的方法时,需要使用()
:
$this->makeHidden(['is_active']);
makeHidden()
方法接受一个参数数组临时设置到模型上的protected $hidden
,当模型转换为JSON
时隐藏它们,以及其他序列化。
我想用 Laravel 创建一个序列化程序。目前,我有我的模型 (CountryEntity
),具有 getSingleCountry()
隐藏特定字段的功能 (is_active
)。
型号
class CountryEntity extends Model
{
public $table = "countries";
protected $fillable = ['id', 'name', 'code', 'language', 'is_active'];
public $timestamps = false;
public function getSingleCountry()
{
$this->makeHidden['is_active'];
return $this;
}
}
控制器
public function show($id)
{
$country = $this->country_repository->getById($id);
$country = $country->getSingleCountry();
return Response::json(['type' => 'success', 'message' => 'Get country',
'data' => $country, 'status' => 200], 200);
}
但是字段 "is_active" 始终可见...
此处的问题在方法调用中缺失 ()
。这一行:
$this->makeHidden['is_active'];
没有明确地做任何事情。有点令人惊讶的是,这并没有引发 Undefined index
错误,但无论如何。
尝试调用Class的方法时,需要使用()
:
$this->makeHidden(['is_active']);
makeHidden()
方法接受一个参数数组临时设置到模型上的protected $hidden
,当模型转换为JSON
时隐藏它们,以及其他序列化。