-1

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?

  • Be more precise in your text, make a pivot table in not the same as filling a pivot table or making records/entries in a pivot table. Or simply seed a pivot table. – N69S Aug 19 '20 at 10:35
  • Does this answer your question? [How to seed pivot table in Laravel 5.4?](https://stackoverflow.com/questions/43554597/how-to-seed-pivot-table-in-laravel-5-4) – N69S Aug 19 '20 at 10:38
  • I need a foreach loop to fill in the pivot table – Georgio Bilani Aug 19 '20 at 10:40
  • Your factories don't seem to be used anywhere, they're kind of useless. Create a new seeder file for your pivot table – brombeer Aug 19 '20 at 10:47

1 Answers1

0

Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use model factories to conveniently generate large amounts of database records. First, review the model factory documentation to learn how to define your factories. Once you have defined your factories, you may use the factory helper function to insert records into your database.

For example, let's create 50 abilities and attach a relationship to each model:

<?php

use App\Ability;
use App\Model;
use Illuminate\Database\Seeder;

class AbilitySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Ability::class, 50)->create()->each(function ($ability) {
            $ability->model()->attach(factory(Model::class, rand(1, 5))->create());
        })
    }
}

Of course, as mentioned above, you will need to define your factories correctly for this to work.

In addition, you will need to define the relationship to the Model correctly in your Ability model.

MrEduar
  • 1,784
  • 10
  • 22