Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Created March 9, 2018 11:17
Show Gist options
  • Save jsdecena/9e8e31b6126b646a2d4331d37f07d0b4 to your computer and use it in GitHub Desktop.
Save jsdecena/9e8e31b6126b646a2d4331d37f07d0b4 to your computer and use it in GitHub Desktop.
Many to Many Table Seeder
<?php
use App\Shop\Categories\Category;
use App\Shop\Products\Product;
use Illuminate\Database\Seeder;
class CategoryProductsTableSeeder extends Seeder
{
public function run()
{
factory(Category::class, 5)->create()->each(function (Category $category) {
factory(Product::class, 5)->make()->each(function(Product $product) use ($category) {
//If you have other columns in the pivot table, do this
$category->products()->save($product, ['column_name' => 'value']);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment