Laravel 5 拉维多对多连接?
我试图创建或附加数据到名为“用户投票”的表中,但我不知道为什么“投票”和“用户”之间的连接不起作用 用户投票表Laravel 5 拉维多对多连接?,laravel-5,Laravel 5,我试图创建或附加数据到名为“用户投票”的表中,但我不知道为什么“投票”和“用户”之间的连接不起作用 用户投票表 Schema::create('user_votes', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('user_id')->unsigned(); $table->bigInteger('vote_id
Schema::create('user_votes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('vote_id')->unsigned();
$table->string('name');
$table->string('page', 100)->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('vote_id')->references('id')->on('votes');
});
投票模式:
public function user_votes()
{
return $this->belongsToMany(User::class, 'user_votes');
}
用户模型:
public function user_votes()
{
return $this->belongsToMany(Vote::class, 'user_votes');
}
商店管理员:
auth()->user()->user_votes()->attach([
'name' => $option->name,
'page' => $option->page
]);
谢谢你的建议。请尝试下面的代码,希望它能帮助你
$voteId = 1;
auth()->user()->user_votes()->attach($voteId => [
'name' => $option->name,
'page' => $option->page
]);