我在Laravel中创建数据透视表时遇到问题。这是我第一次使用它,并且在Internet上搜索后,我无法设法解决问题。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'champions_teams.teams_id' in 'field list' (SQL: select `champions`.*, `champions_teams`.`teams_id` as `pivot_teams_id`, `champions_teams`.`champions_id` as `pivot_champions_id`, `champions_teams`.`champion_id` as `pivot_champion_id` from `champions` inner join `champions_teams` on `champions`.`id` = `champions_teams`.`champions_id` where `champions_teams`.`teams_id` = 1) (View: C:\laragon\www\proyecto-web\resources\views\teams\teamIndex.blade.php)
以下,是我通过数据透视表迁移创建类“冠军”和“团队”的方式。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\softDeletes;
class Teams extends Model
{
use SoftDeletes;
protected $table = 'teams';
protected $fillable = ['user_id','name','rank','region'];
public function user(){
return $this->belongsTo(User::class);
}
public function champions(){
return $this->belongsToMany(Champions::class)->withPivot('champion_id');
}
public function files(){
return $this->morphMany(File::class, 'model');
}
public function setNameAttribute($value){
$this->attributes['name'] = strtoupper($value);
}
public function getTeamsNameAttribute(){
return $this->name;
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Champions extends Model
{
protected $table = 'champions';
protected $fillable = ['name','health_points','type','role'];
public function teams(){
return $this->belongsToMany(Teams::class)->withPivot('team_id');;
}
public function items(){
return $this->hasMany(Items::class, 'champion_id');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class PivotTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('champions_teams', function(Blueprint $table){
$table->unsignedBigInteger('champion_id');
$table->unsignedBigInteger('team_id');
$table->foreign('champion_id')
->references('id')
->on('champions')
->onDelete('cascade');
$table->foreign('team_id')
->references('id')
->on('teams')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
感谢您的帮助!您也可以在此处访问整个代码:https://github.com/ValentinDelpy/proyecto-web
[Laravel可能是从Model名称生成ID的,所以如果Project是全新的,只需对其进行更改,否则,您必须在belogsToMany
函数中声明外键名称(检查https://laravel.com/api/6.x/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_belongsToMany)。
还请记住,witPivot
功能是让Laravel知道其他字段的存在,而不仅仅是两个外键,而不是它们自己的外键(检查https://laravel.com/api/6.x/Illuminate/Database/Eloquent/Relations/BelongsToMany.html#method_withPivot)
Many to Many由于您未遵循Laravel期望的名称约定,因此应通过将附加参数传递给belongsToMany
方法来自定义联接表的名称,即表上键的列名称。第三个参数是您要在其上定义关系的模型的外键名称,而第四个参数是您要加入的模型的外键名称:
class Champions extends Model
{
public function teams(){
return $this->belongsToMany(Teams::class, 'champions_teams', 'team_id', 'champion_id');
}
}
class Teams extends Model
{
public function champions(){
return $this->belongsToMany(Champions::class, 'champions_teams', 'champion_id', 'team_id');
}
}