Ask Your Question
3

How to deal with situations when the parent Eloquent model both exists and does not exist?

asked 2021-07-27 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-08-14 22:00:00 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-27 11:00:00 +0000

Seen: 10 times

Last updated: Aug 14 '22