title: Laravel ManytoMany: How to attach a product to a user upon user registration?
body: <p>I am new to Laravel and I am trying to create an application for our customers to confirm ownership of their products.
  I created the following tables and models: </p>
  
  <p>users table</p>
  
  <pre><code>Schema::create('users', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
      $table-&gt;string('name');
      $table-&gt;string('email')-&gt;unique();
      $table-&gt;timestamp('email_verified_at')-&gt;nullable();
      $table-&gt;string('password');
      $table-&gt;rememberToken();
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p>products table</p>
  
  <pre><code>Schema::create('products', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
      $table-&gt;string('name');
      $table-&gt;integer('etc');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p>user_product table</p>
  
  <pre><code>Schema::create('user_product', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
  
  $table-&gt;unsignedBigInteger('user_id')-&gt;index();
  $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
  
  $table-&gt;unsignedBigInteger('product_id')-&gt;index();
  $table-&gt;foreign('product_id')-&gt;references('id')-&gt;on('products')-&gt;onDelete('cascade');
  
  $table-&gt;timestamps();
  
  });
  </code></pre>
  
  <p>user model</p>
  
  <pre><code>public function products()
   {
      return $this-&gt;belongsToMany('App\Product', 'user_product')-&gt;withTimestamps();
   }
  </code></pre>
  
  <p>product model</p>
  
  <pre><code>public function users()
  {
      return $this-&gt;belongsToMany('App\User');
  }
  </code></pre>
  
  <p>So here’s where I’m stuck…
  I want to have a product displayed together with the user registration form.  Upon registration, the user has to confirm ownership of the product. So I need to associate the product displayed with the user account that is about to be created. Not sure how to do that though…</p>
  
  <p>I am using ManyToMany because I want to have all the products in the database, even if they belong to a user or not. After the association between the user and the product, you cannot access that product without being logged in. 
  I hope it makes a bit of sense, still working on the logistics behind this :) </p>
  
  <p>Any idea of how to reach this scenario?
  Any help would be much appreciated :) </p>