Ad

Problem Relationship Many To Many On Laravel

- 1 answer

I'm new to Laravel, I'm having this problem: I have 2 tables, platos and ingredientes, these have a many to many relationship, and for this I use a third table called ingredientes_platos.

To save the relationship many to many I tried with the following:

$platos->ingredientes()->attach($input['ingredientes']);

but it gives the following error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '151-3' for key 'PRIMARY' (SQL: insert into ingredientes_platos (platos_id, ingredientes_id, norma_bruta) values (151, 3, ))

Looking a bit in the documentation, I could solve with sync instead of attach, but this does not solve my problem, because in addition to saving the id's of the relationship I need to save other attributes in the pivot table.

It is important to note that if I try to save this data in a table other than ingredients_platos, I do not get this problem and the data is saved correctly no matter which method I use.

I appreciate your attention, I hope you can help me.

These are the models for the three tables:

Table Platos:

public $table = 'platos';


protected $dates = ['deleted_at'];


public $fillable = [
    'Grupo',
    'Nombre',
    'Procedimiento',
    'Cantidad',
    'Unidad',
    'Precio'
];

/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    'Grupo' => 'integer',
    'Nombre' => 'string',
    'Procedimiento' => 'string',
    'Cantidad' => 'integer',
    'Unidad' => 'integer',
    'Precio' => 'double'
];

/**
 * Validation rules
 *
 * @var array
 */
public static $rules = [
    'Grupo' => 'required',
    'Nombre' => 'required'
];

public function ingredientes()
{
    return $this->belongsToMany(Ingredientes::class);
}

public function grupo_platos()
{
    return $this->hasOne('App\Models\Grupo_Platos', 'id', 'Grupo');
}

}

Table Ingredientes:

public $table = 'ingredientes';

protected $dates = ['deleted_at'];


public $fillable = [
    'Grupo',
    'Nombre',
    'Descripcion',
    'Kcal',
    'Proteinas',
    'Grasas',
    'Unidad',
    'Precio'
];

/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    'Grupo' => 'integer',
    'Nombre' => 'string',
    'Descripcion' => 'string',
    'Kcal' => 'double',
    'Proteinas' => 'double',
    'Grasas' => 'double',
    'Unidad' => 'integer',
    'Precio' => 'double'
];

/**
 * Validation rules
 *
 * @var array
 */
public static $rules = [
    'Nombre' => 'required'
];

public function platos()
{
    return $this->belongsToMany(Platos::class);
}

}

Table Ingredientes_Platos:

public $table = 'ingredientes_platos';

public $fillable = [
    'platos_id',
    'ingredientes_id',
    'norma_bruta',
    'norma_neta',
    'unidad_id'
];

public $timestamps = false;

}

Platos Controller:

public function store(CreatePlatosRequest $request)
{
    $input = $request->all();

    $platos = $this->platosRepository->create($input); 
    $id = $platos->id;

    $ingredientes = $input['ingredientes'];
    $norma_b = $input['norma_b'];

    $t = sizeof($ingredientes);

    $i=0;

    for ($i = 0; $i < $t; $i++) { 

        $pivot = new Ingredientes_Platos;

        $pivot->platos_id = $platos['id'];
        $pivot->ingredientes_id = $ingredientes[$i];
        $pivot->norma_bruta = $norma_b[$i];

        $pivot->save();
    }


    Flash::success('Plato agregado correctamente.');

    return redirect(route('platos.index'));
}
Ad

Answer

Thank you very much, my problem was solved. It happens that in addition to Laravel, I'm using InfyOM and this was creating a conflict when I was trying to manually add the records to the pivot table, because this line was already responsible for adding the registry to the pivot table: $ dishes = $ this-> dishesRepository-> create ($ input);

Ad
source: stackoverflow.com
Ad