title: Laravel firstOrCreate() method does not work on relations
body: <p>I want to use the method <code>firstOrCreate()</code> through relationships, for which I encounter the following error.</p>
  <blockquote>
  <p>Call to a member function firstOrCreate() on null</p>
  </blockquote>
  <p><strong>Controller</strong></p>
  <pre><code>public function store(Request $request)
  {
      $data = $request-&gt;validate([
          'categories' =&gt; 'required',
          'attributes' =&gt; ['required', 'array']
      ]);
  
      $product = auth()-&gt;user()-&gt;products()-&gt;create($data);
      $product-&gt;categories()-&gt;sync($data['categories']);
  
      // For Attributes
      $attributes = collect($data['attributes']);
      $attributes-&gt;each(function ($item) use ($product) {
          if (is_null($item['name']) || is_null($item['value'])) return;
  
          $attr = Attribute::firstOrCreate(
              ['name' =&gt; $item['name']]
          );
          // the error for line 63
          $attr_value = $attr-&gt;values()-&gt;firstOrCreate(
              ['value' =&gt; $item['value']]
          );
  
          $product-&gt;attributes()-&gt;attach(['attribute_id' =&gt; $attr-&gt;id, 
              ['value_id' =&gt; $attr_value-&gt;id]]);
      });
  
      return redirect(route('admin.products.index'));
  }
  </code></pre>