aem99
1 month ago
672
30
Eloquent

What to return if no record was found

Posted 1 month ago by aem99

Hi,

I have a scenario where I have a helper function that looks up latest product and returns it. As this function is called in few places I have placed it in its own file and class. Normally that function will return a instance (record) which is the latest product. And in few places I use the with function to load up some relationships. However, currently when there are no products, the function returns null, and when I can with on null it error out. This could be easily mitigated by just checking if the value is null, and don't perform the with command if it's, however that will require me to change the function call in every place the with command is used in congustion with my helper function, which is not ideal.

So what I am asking is, what is the Eloquent way to deal with such a scenario, if any.

image

Thanks!

Please sign in or create an account to participate in this conversation.