0
所以我有一个模型,我从JSON填充。如果我只是从控制器返回填充模型,所有工作都按预期工作。但是,如果我尝试访问/返回/使用模型的特定属性,它将返回默认值。我觉得我必须在这里错过一些基本的东西。任何帮助表示赞赏。雄辩的填充,但没有数据
class parent extends Model
{
$id = -1;
$child;
protected $fillable = ['id','child'];
public function __construct()
{
$child = new child();
}
}
class child extends Model
{
$id = -1;
protected $fillable = ['id'];
}
然后在控制器的方法
public function doStuff(Request $request)
{
$data = json_decode($request->input('parent'),true);
$newParent = new parent();
$newParent->fill($data);
return $newParent; //has data as expected from posted json data which includes a parent with a child underneath it and data set on both
return $newParent->id; //has -1 rather than passed in value
}