我在更新與 graphQL 查詢重命名的關系時遇到問題。以下是相關的架構和Laravel模型:拉拉維爾模型<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;use Illuminate\Database\Eloquent\Relations\BelongsToMany;class Lead extends Model{ /** * The attributes that are mass assignable. * * @var array */ // protected $fillable = [ // 'lead_type_id', // ]; protected $guarded = []; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ // ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ // 'created_at' => 'timestamp', // 'updated_at' => 'timestamp' ]; /** * Get the LeadActions for the Lead. */ public function leadActions() { return $this->hasMany(\App\Models\LeadAction::class); } /** * Get the clients for the Lead. */ public function clients(): BelongsToMany { return $this->belongsToMany(Client::class); } /** * Get the LeadType for the Lead. */ public function leadType(): BelongsTo { return $this->belongsTo(\App\Models\LeadType::class, 'lead_type_id'); }}?><?phpnamespace App\Models;use App\Models;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\HasMany;class LeadType extends Model{ /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ // ];我遵循了Laravel命名約定,并將列命名為lead_type_id在潛在客戶表上。如果我刪除將lead_type關系重命名為 leadType,我可以成功運行更新突變,但我無法弄清楚如何讓它使用列的正確名稱(lead_type_id),同時保持關系重命名。任何幫助都非常感謝。
1 回答

萬千封印
TA貢獻1891條經驗 獲得超3個贊
你試過指令嗎?我的意思是,你必須在你的中使用它,因為燈塔尋找名為的關系,并且由于沒有定義,它假設這是一個參數。@renamelead_typeUpdateLeadInputlead_typelead_type
在模型中重命名關系,例如:
class Lead extends Model
{
public function lead_actions()
{
return $this->hasMany(\App\Models\LeadAction::class);
}
public function lead_type(): BelongsTo
{
return $this->belongsTo(\App\Models\LeadType::class, 'lead_type_id');
}
}
或
使用指令(我沒有嘗試過,但我的意思是它的工作原理是這樣的):@rename
input UpdateLeadInput {
id: ID!
clients: UpdateClientsRelation
lead_type: UpdateLeadTypeRelation @rename(attribute: "leadType")
}
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消