title: How does Laravel optional() work?
body: <p>When creating new models, I am trying to use the <code>optional()</code> helper to assign values when they are set. However, when the <code>user</code> object isn't set in this context, I receive this error: <code>"Undefined property: stdClass::$user"</code></p>
  
  <pre><code>$this-&gt;events()-&gt;create([
      'category_a' =&gt; optional($event)-&gt;categoryA,
      'category_b' =&gt; optional($event)-&gt;categoryB,
      'user' =&gt; optional($event-&gt;user)-&gt;accountId,
  ]);
  </code></pre>
  
  <p>To clarify, the <code>$event</code> is always set, however it can contain different values. Sometimes <code>category_a</code> is set, sometimes <code>category_b</code> is set. The optional helper seems to do the trick here. But, when the object is deeper than one level, it throws an error.</p>
  
  <p>So how do I work with the <code>optional</code> helper correctly using deeper than one level objects?</p>