gpt4 book ai didi

php - Laravel 的数据透视表问题

转载 作者:行者123 更新时间:2023-11-29 15:26:30 25 4
gpt4 key购买 nike

我在 Laravel 中创建数据透视表时遇到问题。这是我第一次使用它,在网上搜索后,我无法解决我的问题。

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

最佳答案

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');
}

}

关于php - Laravel 的数据透视表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com