laravel - Laravel 上的多对多问题关系
问题描述
我是 Laravel 的新手,我遇到了这个问题:我有 2 个表,platos 和配料,它们有一个多对多的关系,为此我使用了第三个表,称为 ingredients_platos。
为了挽救多对多的关系,我尝试了以下方法:
$platos->ingredientes()->attach($input['ingredientes']);
但它给出了以下错误:
SQLSTATE [23000]:完整性约束违规:1062 键“PRIMARY”的重复条目“151-3”(SQL:插入ingredientes_platos
(platos_id
、、ingredientes_id
)norma_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'));
}
解决方案
// 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();
}
}
推荐阅读
- python - 如何扩展字典中的现有键?
- selenium-webdriver - 我需要一个 gradle 任务来执行我的 Cucumber Runner 类并执行 Cucumber tets
- python - Python遥测 - 记录所有加载的python模块
- go - 无论平台如何,如何在所有代码上运行 go vet
- r - 导入csv文件的麻烦
- angular - 如何在支持 i18n 的 Microsoft IIS 上部署 Angular 10 应用程序
- python - TKINTER:选择某个下拉值时,如何制作按钮做其他事情?
- git - 有没有办法将 Git 提交历史作为文件系统来浏览?
- typescript - 打字稿打字相关类型
- javascript - Jquery-仅使用具有类属性的第一个元素动态创建 ul