I need help with making a pivot table for all entires using for each in Laravel
I have the following three tables
Abilities ability_model Models
+----------------+ +----------------------+ +-----------------+
| id | name | |ability_id| model_id | | id | name |
+----------------+ +----------------------+ +-----------------+
| 1 | View | | | | | 1 | Question |
|----------------| |----------------------| |-----------------|
| 2 | Create | | | | | 2 | Answer |
|----------------| |----------------------| |-----------------|
| 3 | Update | | | | | 3 | User |
|----------------| |----------------------| +-----------------+
| 4 | Delete | | | |
+----------------+ +----------------------+
How can make the pivot table using factories and seeders
This is my Ability Factory:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Model;
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
return [
//
];
});
This is my Ability Seeder
<?php
use App\Ability;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class AbilitySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('abilities')->insert([
'name'=>'Delete'
]);
}
}
This is my Model Factory:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Model;
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
return [
//
];
});
This is my Model Seeder:
<?php
use App\Model;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class ModelSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('models')->insert([
'name'=>'Question'
]);
}
}
Where should I add my foreach loop and how should I write it in order to have each of the abilities assigned to each model?