title: Laravel : Create new product with category in database
body: <p>for the moment, i'am creating my product link to my category directly from my code in my seed doing 
  <code>-&gt;categories()-&gt;attach(1)</code> at the end of each product.</p>
  
  <p>From my database, I can create a product but i can't link them with a foreign key to a category that is already in <code>category_product_table</code>. </p>
  
  <p>I have 3 table : <code>products</code>, <code>categories</code> and <code>category_product</code>. </p>
  
  <p>2020_04_09_073846_create_products_table</p>
  
  <pre><code>    public function up()
      {
          Schema::create('products', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('category_id')-&gt;unsigned()-&gt;index();
              $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
              $table-&gt;string('name');
              $table-&gt;string('slug');
              $table-&gt;string('category');
              $table-&gt;string('description');
              $table-&gt;string('releaseDate');
              $table-&gt;float('price');
  
              $table-&gt;timestamps();
          });
      }
  </code></pre>
  
  <p>ProductSeeder</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Seeder;
  use App\Product;
  
  class ProductSeeder extends Seeder
  {
      /**
       * Run the database seeds.
       *
       * @return void
       */
      public function run()
      {
  
              Product::create([
                  'name' =&gt; 'Halo 5',
                  'slug' =&gt; 'halo-5',
                  'category_id' =&gt; '1',
                  'category' =&gt; 'Xbox One',
                  'description' =&gt; "Halo 5: Guardians sur Xbox One est un FPS mettant en scène les aventures du Master Chief et d'un nouveau personnage, le Spartan Jameson Locke. ",
                  'releaseDate' =&gt; '27 octobre 2015',
                  'price' =&gt; '54.99'
  
              ]);
  </code></pre>
  
  <p>2020_05_02_201337_create_categories_table</p>
  
  <pre><code>    public function up()
      {
          Schema::create('categories', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name')-&gt;unique();
              $table-&gt;string('slug')-&gt;unique();
              $table-&gt;timestamps();
          });
      }
  </code></pre>
  
  <p>CategorieSeeder</p>
  
  <pre><code>&lt;?php
  
  use Carbon\Carbon;
  use App\Category;
  use Illuminate\Database\Seeder;
  
  class CategorieSeeder extends Seeder
  {
      /**
       * Run the database seeds.
       *
       * @return void
       */
      public function run()
      {
          $now = Carbon::now()-&gt;toDateTimeString();
  
          DB::table('categories')-&gt;insert(
              array(
                  array( 
                      'name' =&gt; 'Xbox',
                      'slug' =&gt; 'xbox',
                  ),
                  array(
                      'name' =&gt; 'Playstation',
                      'slug' =&gt; 'playstation',
                  ),
                  array(
                      'name' =&gt; 'PC',
                      'slug' =&gt; 'pc',
                  ),
                  array(
                      'name' =&gt; 'Switch',
                      'slug' =&gt; 'switch',
                  ),
              )
          );
      }
  }
  </code></pre>
  
  <p>2020_05_03_105839_create_category_product_table</p>
  
  <pre><code>    public function up()
      {
          Schema::create('category_product', function (Blueprint $table) {
  
              $table-&gt;increments('id');
              $table-&gt;integer('product_id')-&gt;unsigned()-&gt;nullable();
              $table-&gt;foreign('product_id')-&gt;references('id')-&gt;on('products')-&gt;onDelete('cascade');
  
              $table-&gt;integer('category_id')-&gt;unsigned()-&gt;nullable();
              $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
  
              $table-&gt;timestamps();
          });
      }
  </code></pre>
  
  <p>Product.php</p>
  
  <pre><code>class Product extends Model
  {
      public function categories()
      {
          return $this-&gt;AsOne('App\Category');
      }
  }
  </code></pre>
  
  <p>Category.php</p>
  
  <pre><code>class Category extends Model
  {  
      public function products()
      {
          return $this-&gt;belongsToMany('App\Product');
      }
  }
  </code></pre>
  
  <p>HomeController</p>
  
  <pre><code>    public function public(){
  
          if (request()-&gt;category) {
              $home = Product::with('categories')-&gt;whereHas('categories', function ($query){
                  $query-&gt;where('slug', request()-&gt;category);
              })-&gt;get();
              $categories = Category::all();
              $categoryName = $categories-&gt;where('slug', request()-&gt;category)-&gt;first()-&gt;name;
          } else {
  
          $home = Product::inRandomOrder()-&gt;paginate(9);
          $categories = Category::all();
          $categoryName = 'Featured';
  
          }
          return view('home.index')-&gt;with([
              'home' =&gt; $home,
              'categories' =&gt; $categories,
              'categoryName' =&gt; $categoryName,
              'mode' =&gt; 'public'
          ]);
  </code></pre>
  
  <p>If someone can help me, thanks for your help !</p>