title: Laravel Many-to-Many relationship with UUID as primary
body: <p>Hi I have 2 models with relationship - <strong>Users</strong> and <strong>Chats</strong>.<br>
  Model <strong>Chats</strong> has an <strong>id(uuid)</strong> field as a primary key:</p>
  
  <pre><code>public $primaryKey = 'id';
  
  public $keyType = 'string';
  
  public $incrementing = false;
  
  public function users()
  {
      return $this-&gt;belongsToMany(User::class, 'chat_user', 'chat_id', 'user_id');
  }
  </code></pre>
  
  <p>Users:</p>
  
  <pre><code>public function chats()
  {
      return $this-&gt;belongsToMany(Chats::class, 'chat_user', 'user_id', 'chat_id');
  }
  </code></pre>
  
  <p>pivot table:</p>
  
  <blockquote>
    <p>id | chat_id(uuid) | user_id (int)</p>
  </blockquote>
  
  <p>When I trying to sync user with chat I getting not null violation, because my chat_id is empty:</p>
  
  <blockquote>
    <p>Failing row contains (1, null, 1)<br>
    ---------------------------------^</p>
  </blockquote>
  
  <p>Adding <code>protected $casts = ['id' =&gt; 'string'];</code> to Chats model did not resolve the problem. What i'm doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My store method:</p>
  
  <pre><code>$chat = $this-&gt;chatRepository-&gt;create([
          'name' =&gt; $request-&gt;name,
          'creator_id' =&gt; Auth::id(),
      ]); // also, chat succesfully created at this step but if I'm checking dd($chat) - it doesnt contain 'id' in attributes array...
  
      $memberIds = $this-&gt;userRepository
          -&gt;findWhereIn('email', collect($request-&gt;members)-&gt;pluck('email')-&gt;toArray(), ['id']);
  
      $chat-&gt;users()-&gt;sync($memberIds); &lt; error at this step
  </code></pre>