Laravel多对多关系无法建立一种模型,而不能建立另一种模型

时间:2020-02-02 17:34:36

标签: php laravel eloquent many-to-many

我有一个Laravel应用程序的开始,它只有两个模型:User和Project。 用户与项目之间存在多对多的关系,一个用户可以处理多个项目,而一个项目可以由多个用户处理。

我遇到的问题以两种方式表现出来:

  1. 使用基本数据迁移数据库并为其播种后,我在project_user数据透视表中手动创建一行,将引用链接到usersprojects表的第一项: {1}}。然后,我启动Tinker,以测试这种关系。通过用户模型访问所有项目均按预期工作:INSERT INTO project_user (user_id, project_id) VALUES (1, 1)(返回有效的项目模型集合),但是,相反的关系:User::find(1)->projects在应返回用户模型时返回空集合。在数据透视表中引用。
  2. 并且,当使用Seeder文件播种Project::find(1)->users表时,如下所示:
project_user

这会导致种子文件失败并出现以下错误:public function run() { $project = Project::first(); $admin = User::first(); if (isset($project) && isset($admin)) { echo "Project: $project\n"; echo "User: $admin\n"; // FIXME: Why isn't this working? $project->users()->attach($admin); // The inverse below also fails for the same reason // $admin->projects()->attach($project); } } project_user SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null (SQL: insert into created_at ( project_id , updated_at , user_id , < / p>

我已经搜寻了我的代码,以及在互联网上寻找解决方案,但都无济于事。问题可能是我的代码中有一些非常微小的错误,某些函数的使用不正确或操作顺序无效。对我来说很明显,项目方面的关联肯定有问题,但是我逐字逐句地比较了用户和项目,但我不知道出了什么问题。我将在下面粘贴相关的代码部分,如果可以提供其他信息或代码,我非常乐意这样做。预先感谢您的帮助!

以下是模型定义:

) values (2020-02-02 17:01:04, ?, 2020-02-02 17:01:04, 1))
class User extends Authenticatable
{
    public $type;

    // Here is the relationship to the Projects this User is working on
    public function projects()
    {
        // SELECT * FROM project_user WHERE user_id = $user->id
        return $this->belongsToMany(Project::class)->withTimestamps();
    }


    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

class Project extends Model { public $id; public $name; public $deleted_at; // Here is the relationship to the Users working on this Project public function users() { // SELECT * FROM project_user WHERE project_id = $project->id return $this->belongsToMany(User::class) ->withTimestamps(); } protected $fillable = [ 'name', ]; protected $casts = [ 'deleted_at' => 'datetime', ]; } users表迁移:

projects
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->enum('type', ['admin', 'client', 'editor'])->default('client');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

这是设置引用的数据透视表public function up() { Schema::create('projects', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name')->unique(); $table->integer('memberCount')->default(0)->nullable(false); $table->timestamp('deleted_at')->nullable(); $table->timestamps(); }); } 迁移:

project_user

1 个答案:

答案 0 :(得分:1)

请尝试从public $id;模型(和其他冗余属性)中删除Project。使用Eloquent,您无需声明作为模型支持表中的列存在的模型属性。我相信正在发生的事是调用了$id模型上的Project属性,该属性为null,因为它覆盖了Eloquent开箱即用为该列提供的神奇的吸气剂。

如果要在模型上声明属性,以便IDE可以选择它们,请考虑使用barryvdh/laravel-ide-helper包。