4

Say I have a model called Fruit which can have a null value for seeds. The seeds column exists in the database table.

The problem is that using isset is returning false for this model property when it is set to null, or maybe it has something to do with the __magic stuff in general? So how do I check if the model has the property seeds even when it is set to null or declared via magic?

For example:

$fruit = Fruit::find(1);

if (isset($fruit->seeds)) {
  echo 'Yes';
}
else {
  echo 'No';
}

This will echo No if the value of seeds is set to null in the database.

2
  • 1
    Add the condition is_null($fruit->seeds). Commented Feb 14, 2018 at 10:30
  • Possible duplicate of Correct way to use !is_null() Commented Mar 20, 2019 at 19:42

1 Answer 1

9

You can use is_null():

if (isset($fruit->seeds) && !is_null($fruit->seeds)) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.