laravel多对多关系未存储在透视表中

laravel多对多关系未存储在透视表中,laravel,eloquent,pivot,relationship,eloquent-relationship,Laravel,Eloquent,Pivot,Relationship,Eloquent Relationship,我有两个表developer table和skill table,它们与多对多关系developer\u skill table链接。我想在pivot表中存储开发者id和技能id 但当开发人员添加技能并提交时,返回错误 在开发人员表中添加数据,但在透视表中未添加数据。 错误 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'developer_id' cannot be null (SQL: insert into `de

我有两个表developer table和skill table,它们与多对多关系developer\u skill table链接。我想在pivot表中存储开发者id和技能id

但当开发人员添加技能并提交时,返回错误

在开发人员表中添加数据,但在透视表中未添加数据。 错误

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'developer_id' cannot be null (SQL: insert into `developer_skill` (`developer_id`, `skill_id`) values (, 5))
用于存储数据的开发人员控制器

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Developer; 
use App\Skill as skill;

class developerController extends Controller
{
    public function __construct(Developer $Developer, skill $skill)
    {
        $this->Developer = $Developer;
        $this->skill = $skill;
    }

     public function store(Request $request)
      {
       $skills = implode(',', $request->user_skills);
    
          $data = array(
                'name' => $request->name,
                'skills' => $skills,
            );
    
            if ($this->Developer->create($data)) {
                $syncTagData = array();
                if (!empty($request->user_skills)) {
                    $syncTagData = $request->user_skills;
                }
    
                $this->Developer->user_skills()->sync($syncTagData);
            }
}
}
开发人员技能

developer_id
skill_id
public function users()
{
    return $this->belongsToMany(Developer::class);
}
public function user_skills()
    {
        return $this->belongsToMany('App\Skill');

}
技能模型

developer_id
skill_id
public function users()
{
    return $this->belongsToMany(Developer::class);
}
public function user_skills()
    {
        return $this->belongsToMany('App\Skill');

}
开发人员模型

developer_id
skill_id
public function users()
{
    return $this->belongsToMany(Developer::class);
}
public function user_skills()
    {
        return $this->belongsToMany('App\Skill');

}
你的问题是:

if ($this->Developer->create($data)) {
    $syncTagData = array();
    if (!empty($request->user_skills)) {
        $syncTagData = $request->user_skills;
    }

    $this->Developer->user_skills()->sync($syncTagData);
}
$this->Developer
已经是
Developer
的实例,因此当您调用
create()
时,它生成的查询不是您所期望的

您有两个选择:

使用模型外观(我的首选):

使用上述方法,您应该能够删除您的构造函数,因为它不是必需的

或者在
$this->developer
上设置属性并保存:

$this->Developer->name = $data['name'];
$this->Developer->skills = $data['skills'];
if ($this->Developer->save()) {
    $syncTagData = array();
    if (!empty($request->user_skills)) {
        $syncTagData = $request->user_skills;
    }

    $developer->user_skills()->sync($syncTagData);
}

此外,请注意您的用户关系名称。该约定是camelCase,因此您可能会从将其更改为userSkills中获益,否则,当Laravel在幕后发挥其魔力时,您可能会发现奇怪的事情发生。

您在控制器中的
$this->developer
设置在哪里?你能在你的控制器中显示整个方法,包括声明和任何构造函数吗?好的,我正在更新问题我已经更新了我的控制器。查看你的路线可能也会有帮助。从本质上看,
$this->developer
是一个新的
开发人员
而不是从数据库中解析的开发人员。这就是ID为空的原因。对不起,我刚刚意识到我的答案太离谱了。我完全误读了你的代码!我再看一看不客气,我希望它能解决你的问题。这是工作,我已经解决了我的问题,非常好!你能帮我把答案标记为已接受吗?我渴望声誉积分:)我已经通过不展示将其标记为已接受,我认为我的声誉不到15Ah,不用担心。很高兴我能帮上忙