8040

Question:
i am trying to get path of uploaded file using fileupload widget and then copying that file in custom folder but when creating new record it gives error <strong>trying to get property 'path' of non-object</strong> when afterSave() calls.
<strong>MODEL:</strong>
public $attachOne = [
'file' => ['System\Models\File']
];
public function afterSave()
{
$path = $this->file->path;
log::info($path);
}
Answer1:replace this afterSave method in your model and it will not show the error you are having.
public function afterSave()
{
$sessionKey = post('_session_key');
$file = $this->file()->withDeferred($sessionKey)->first();
if($file){
log::info($file->getPath());
}
}
the reason is \System\Models\File is available with deferred after master model is commited its changes.
Let me know if you need more help into that.