Laravel supplementary data in pivot table

0

I have a pivot table for a m-n relationship between a travel table and a travelers table. The migration is like this:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomerTourTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customer_tour', function (Blueprint $table) {
            $table->increments('id');
            /** La definición de los campos que se usarán como claves foráneas */
            $table->integer('customer_id')->unsigned();
            $table->integer('tour_id')->unsigned();
            /** La declaración de las claves foráneas en los campos necesarios. */
            $table->foreign('customer_id')->references('id')->on('customers');
            $table->foreign('tour_id')->references('id')->on('tours');
            /** Satisfacción */
            $table->char('satisfaccion', 1);
            /** Los campos de fechas */
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customer_tour');
    }
}

The factory is like this:

<?php

use Faker\Generator as Faker;

$factory->define(App\CustomerTour::class, function (Faker $faker) {
    return [
        'satisfaccion' => $faker->randomElement(['A', 'B', 'C'])
    ];
});

And the model is like this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;

class CustomerTour extends Pivot
{
    use SoftDeletes;
    protected $table = 'customers';
    protected $fillable = [
        'satisfaccion'
    ];
    protected $guarded = [
        'id'
    ];
    public $timestamps = true;
    protected $dates = [
        'deleted_at'
    ];
}

I have the problem in the seeder. I do not know how to do that, in each relation that is created, the field satisfaccion is filled according to the randomElement that is indicated in the factory. The foreign keys are filled in well, but the field satisfaccion I do not know how to include it, and in the documentation does not come any of this.

The seeder is like this:

<?php

use App\Operator;
use App\Tour;
use App\Customer;
use App\CustomerTour;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        factory (Operator::class, 20)->create();

        /** Los customers que se crean se asignan a una colección
         * en memoria, para poder usarlos para crear las relaciones
         * con los tours. */
        $customers = factory (Customer::class, 300)->create();

        factory (Tour::class, 100)
        ->create()
            ->each(function ($tour) use ($customers)
                {
                    $tour->customers()
                        ->attach($customers
                            ->random(mt_rand(10, 40))
                            ->pluck('id')
                        );
                }
            );
    }
}

Does anyone know how to do this so that, as relationships are filled, that field is completed? I would also need to know how to manage it later from the controllers.

Thank you very much.

    
asked by Chefito 03.12.2018 в 17:19
source

0 answers