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
}