首页 > 解决方案 > Laravel 上的多对多问题关系

问题描述

我是 Laravel 的新手,我遇到了这个问题:我有 2 个表,platos 和配料,它们有一个多对多的关系,为此我使用了第三个表,称为 ingredients_platos。

为了挽救多对多的关系,我尝试了以下方法:

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

但它给出了以下错误:

SQLSTATE [23000]:完整性约束违规:1062 键“PRIMARY”的重复条目“151-3”(SQL:插入ingredientes_platosplatos_id、、ingredientes_idnorma_bruta值(151、3、))

在文档中查看了一下,我可以用同步而不是附加来解决,但这并不能解决我的问题,因为除了保存关系的 id 之外,我还需要在数据透视表中保存其他属性。

需要注意的是,如果我尝试将这些数据保存在 components_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 = '配料';

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);
}

}

表成分_柏拉图:

public $table = 'ingredientes_platos';

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

public $timestamps = false;

}

柏拉图控制器:

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'));
}

标签: laravelmany-to-manyrelationship

解决方案


// Many to Many relationship in category Model to Post model coding here
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
        public function posts()
    {
        return $this->belongsToMany('App\Post')->withTimestamps();
    }
}

// Many to Many relationship in Post Model to Category model coding here

namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{

    public function categories(){
return $this->belongsToMany('App\Category')->withTimestamps();
 }




}

推荐阅读