Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel以多对多方式插入数据并获取错误_Php_Laravel - Fatal编程技术网

Php Laravel以多对多方式插入数据并获取错误

Php Laravel以多对多方式插入数据并获取错误,php,laravel,Php,Laravel,在我的应用程序设计中,每个类别都应该有一个或多个用户,我应该将它们关联在一起,中间的类别应该有category\u id引用的categories表和user\u id引用的users表。现在我有了这个迁移文件: 用户: Schema::create('users',函数(Blueprint$表){ $table->id(); $table->foreigned('user_id')->nullable()->constrated(); $table->boolean('active')->默认

在我的应用程序设计中,每个类别都应该有一个或多个用户,我应该将它们关联在一起,中间的类别应该有
category\u id
引用的
categories
表和
user\u id
引用的
users
表。现在我有了这个迁移文件:

用户:

Schema::create('users',函数(Blueprint$表){
$table->id();
$table->foreigned('user_id')->nullable()->constrated();
$table->boolean('active')->默认值(0);//在寄存器中激活帐户
$table->string('name')->nullable();
$table->string('family')->nullable();
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->index()->nullable();
$table->string('password');
$table->rememberToken();
$table->softDeletes();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
类别:

Schema::create('categories',函数(Blueprint$table){
$table->id();
$table->unsignedBigInteger('parent_id')->nullable();
$table->string('title');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
});
用户类别:

Schema::create('user_category',函数(Blueprint$表){
$table->foreigned('category_id')->constrained()->onDelete('cascade');
$table->foreigned('user_id')->constrained()->onDelete('cascade');
$table->primary(
[
“类别_id”,
“用户id”
]
);
});
运行
migrate
命令工作正常,我没有收到任何错误,现在我想创建一个类别,然后创建的类别应该用
user\u category
表和
attach
引用:

$category=\App\Models\category::create(
[
“title”=>“php”,
]
);
$category->owner()->附加(
[
“用户id”=>1
]
);
这里我得到了这个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_user_id' in 'field list'
   (SQL: insert into `user_category` 
      (`category_id`, `category_user_id`) values (4, 1)) 
我的
型号

类别:

public function owner():属于
{
返回$this->belongtomany(CategoryUser::class);
}
用户类别:

public function category():属于
{
返回$this->belongtomany(Category::class);
}

Laravel对透视表的命名约定是以字母顺序用下划线分隔的蛇壳模型名称

因此,如果要遵循约定,则
user\u category
表应改为
category\u user

此外,对于多对多关系,关系应为复数形式

//类别模型
公共函数users():belongtomany
{
返回$this->belongtomany(用户::类);
}
//用户模型
公共函数类别():属于
{
返回$this->belongtomany(Category::class);
}
但是,如果您偏离了约定,也可以通过自定义关系进行设置,如下所示:

//类别模型
公共函数所有者():belongTomany
{
返回$this->belongToMany(用户::class,'User_category','category_id','User_id');
}
//用户模型
公共函数类别():属于
{
返回$this->belongtomany(Category::class,'user_Category','user_id','Category_id');
}

@p-k-tharindu非常感谢,您能解释一下代码的最后一部分吗?我的意思是自定义关系,在类别模型中,为什么您使用
user\u id
作为
relatedPivotKey
,而使用
Category\u id
user
模型中相同,因为这就是您在
user\u Category
透视表中命名外键的原因。这些都在文档中: