Created
March 9, 2018 11:17
-
-
Save jsdecena/9e8e31b6126b646a2d4331d37f07d0b4 to your computer and use it in GitHub Desktop.
Many to Many Table Seeder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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