Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If the parent Eloquent model both exists and does not exist, you should check if the parent model exists before attempting to access it. You could use the exists method of the Eloquent model to check if the parent model exists:

if ($parentModel->exists) {
    // Parent model exists, access it
} else {
    // Parent model does not exist, handle the situation
}

If the parent model does not exist, you could choose to create it or throw an exception. If you choose to create it, you should make sure that you have all the necessary data to create the parent model.