雄辩的关系条件2列外键

时间:2018-01-19 03:49:35

标签: mysql laravel eloquent

我有这样的案例

交易

  

| ID | account_from_id | account_to_id |值|

帐户

  

| ID |名称|类型|

我想使用从表帐户到交易的雄辩关系,它需要附加条件,帐户为来自,或帐户为,但只在一个函数中关系

这是我的代码

public function transactions(){
    $result = $this->hasOne('App\Transactions','id', 'account_from_id');
    if(!is_null($result)){
        return $result;
    }

    return $this->hasOne('App\Transactions','id' 'account_to_id');
}

但它不能像我预期的那样工作,当帐户为时,它将返回null

1 个答案:

答案 0 :(得分:1)

模型列的关系必须在引用表和列上定义 让每列与您的模型有关系

public function getTransactionAttribute(){
   if(!empty($this->account_from_id)){
       return $this->account_from
   }else{
       return $this->account_to
   }
}

按新交易属性

定义帐户交易
     gridView_Attendees.Columns["grade"].Caption = "Grade";
     gridView_Attendees.Columns["grade"].Width = 150;
     gridView_Attendees.Columns["grade"].OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
     gridView_Attendees.OptionsView.RowAutoHeight = true;
     gridView_Attendees.Columns["grade"].AppearanceCell.TextOptions.WordWrap = WordWrap.Wrap;
     RepositoryItemMemoEdit memoEdit = new RepositoryItemMemoEdit();
     memoEdit.ReadOnly = true;
     memoEdit.AutoHeight = true;
     memoEdit.WordWrap = true;
     gridView_Attendees.GridControl.RepositoryItems.Add(memoEdit);
     gridView_Attendees.Columns["grade"].ColumnEdit = memoEdit;