![](/img/trans.png)
[英]Eloquent belongsToMany relation with the not standard column name
[英]Eloquent Intermediate Tabel with None Standard ID Column
在根據 eloquent 標准創建我的數據庫時,我遇到了一個問題,我的table_name和id列名組合起來會超過 64 個字符。
very_long_table_name.very_long_column_name_id
所以我在中間表中使用了一個較短的列名作為外鍵。
遷移文件:
$table->unsignedBigInteger('short_id');
$table->foreign('short_id')->references('id')->on('very_long_table_name');
效果很好,但現在我想插入一個連接
播種機.php:
$x->very_long_table_name()->attach($other_table_name->id);
我收到錯誤消息: SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“very_long_column_name_id”(SQL:插入到very_long_table_name
( just_an_id
, very_long_column_name_id
)值(1、1))
我想要的是它會使用列short_id而不是very_long_column_name_id來插入這個,有沒有什么很酷的方法來解決這個問題? 或者如果我保留長名稱,是否需要手動插入和查詢連接?
就像@Tim 在評論中指出的那樣,這需要在定義關系的 Model VeryLongTableName.php 中完成:
public function very_long_table_name() {
return $this->belongsToMany(Model::class, 'very_long_table_name', 'local_id', 'short_id');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.