Php 拉雷维尔雄辩:多对多关系

Php 拉雷维尔雄辩:多对多关系,php,database,laravel,eloquent,Php,Database,Laravel,Eloquent,我想使用eloquent->attach在我的db表pivot中添加一些项目,但我不明白为什么它不起作用: 我有两种型号(Salade=>(DB中的“salades”表)和Component=>(DB中的“Components”表), 数据透视表是配料沙拉(3列:id、配料沙拉id、沙拉沙拉id) 我的模型: class Ingredient extends Model { protected $table = 'ingredients'; protected $fillable = ['no

我想使用eloquent->attach在我的db表pivot中添加一些项目,但我不明白为什么它不起作用:

我有两种型号(Salade=>(DB中的“salades”表)和Component=>(DB中的“Components”表), 数据透视表是配料沙拉(3列:id、配料沙拉id、沙拉沙拉id)

我的模型:

class Ingredient extends Model
{
protected $table = 'ingredients';

protected $fillable = ['nom'];

public function Salades()
{
    return $this->belongsToMany('App\Salade');
} 
}


class Salade extends Model
{
protected $table = 'salades';

protected $fillable = ['nom','prix'];

public function ingredients()
{
    return $this->belongsToMany('App\Ingredient');
}
}
SaladeController@Store

public function store(Request $request)
{

    $this->validate($request, [
                        'nom' => 'required',
                            'prix' => 'required' ]);


$salade = $request->only(['nom', 'prix']);

// insert new salade in DB
$lanouvelleSalade = \App\Salade::create($salade);

//insert relation in pivot table
$lanouvelleSalade->ingredients()->attach([21,22,23]);

    return redirect('salade')->withOk("Le Salade " . $request->input('name') . " a été modifié.");

}
各表: 新沙拉在表沙拉中插入,但Pivot中的关系不是插入。为什么?

更改:

$lanouvelleSalade->ingredients()->attach([21,22,23]);
致:

更改:

$lanouvelleSalade->ingredients()->attach([21,22,23]);
致:


将您的关系代码替换为此代码并检查:

成分类别:

public function Salades()
{
    return $this->belongsToMany('App\Salade','ingredient_salade','ingredient_id','salade_id');
}
public function ingredients()
{
    return $this->belongsToMany('App\Ingredient','ingredient_salade','salade_id','ingredient_id');
}
和沙拉类:

public function Salades()
{
    return $this->belongsToMany('App\Salade','ingredient_salade','ingredient_id','salade_id');
}
public function ingredients()
{
    return $this->belongsToMany('App\Ingredient','ingredient_salade','salade_id','ingredient_id');
}

如果您有任何错误,请告诉我

用此代码替换您的关系代码并检查:

成分类别:

public function Salades()
{
    return $this->belongsToMany('App\Salade','ingredient_salade','ingredient_id','salade_id');
}
public function ingredients()
{
    return $this->belongsToMany('App\Ingredient','ingredient_salade','salade_id','ingredient_id');
}
和沙拉类:

public function Salades()
{
    return $this->belongsToMany('App\Salade','ingredient_salade','ingredient_id','salade_id');
}
public function ingredients()
{
    return $this->belongsToMany('App\Ingredient','ingredient_salade','salade_id','ingredient_id');
}

如果您有任何错误,请告诉我。

@FredMoras您应该检查Mohammad b的答案以确保正确定义了关系,然后将attach()更改为sync()。@FredMoras您应该检查Mohammad b的答案以确保正确定义了关系,然后将attach()更改为sync().hi mohammad,我有这个错误。SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
tuto
Component\u salade
,constraint
salade\u Component\u Component\u id\u foreign
外键(
Component\u id
)引用
Components
id
)(SQL:insert-into
component\u salade
component\u id
salade\u id
)值(60,21))成分(60)不存在。该列似乎不存在exchanged@FredMoras您确定配料表中有这些id的21,22,23吗?是的,100%,但请查看我的错误:SQL:插入配料沙拉(配料沙拉,沙拉沙拉)值(60,21))在本例中,“21”是第二个值,但成分id是第一列…@FredMoras我更新并更正我的答案检查它again@FredMoras当你想象你发送的一切都正常时,你期待什么?嗨,穆罕默德,我有这个错误。SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
tuto
component\u salade
,约束
salade\u component\u component\u id\u foreign
外键(
component\u id
)引用
components
id
)(SQL:在
配料沙拉
配料沙拉id
配料沙拉id
)值(60,21)中插入配料(60)不存在。该列似乎不存在exchanged@FredMoras您确定配料表中有这些id的21,22,23吗?是的,100%但请查看我的错误:SQL:插入配料沙拉中(配料id,沙拉德id)值(60,21))在本例中,“21”是第二个值,但配料id是第一列…@FredMoras我更新并更正我的答案检查它again@FredMoras当你想象你发送的一切都正常时,你期望什么?