我有一個方法:使用,其僅在子類中定義的方法
protected function chooseAndUpdateParentFight(Fight $fight, Fight $parentFight)
{
$fighterToUpdate = $fight->getParentFighterToUpdate();
...
}
我有2種戰鬥:PreliminaryFight
和DirectEliminationFight
兩者都是Fight
事情子類getParentFighterToUpdate()
的定義只存在於兩個子類中,但不在Fight
類中,所以我得到:
BadMethodCallException in Builder.php line 2443:
Call to undefined method
Illuminate\Database\Query\Builder::getParentFighterToUpdate()
我該怎麼辦?
編輯:這裏都是getParentFighterToUpdate方法:
DirectEliminationFight類別:
public function getParentFighterToUpdate()
{
$childrenGroup = $this->group->parent->children;
foreach ($childrenGroup as $key => $children) {
$childFight = $children->fights->get(0);
if ($childFight->id == $this->id) {
if ($key % 2 == 0) {
return "c1";
}
if ($key % 2 == 1) {
return "c2";
}
}
}
return null;
}
PreliminaryFight類
public function getParentFighterToUpdate()
{
$childrenGroup = $this->group->parent->children;
foreach ($childrenGroup as $key => $children) {
$childFights = $children->fights;
dd($childFights);
// Still writing it
}
return null;
}
編輯2:
/**
* Update fighter if it is possible
* @param $fightsByRound
*/
protected function updateParentFight(Collection $fightsByRound)
{
foreach ($fightsByRound as $fight) {
$parentGroup = $fight->group->parent;
if ($parentGroup == null) break;
$parentFight = $parentGroup->fights->get(0);
$this->chooseAndUpdateParentFight($fight, $parentFight);
}
}
你可以同時顯示'getParentFighterToUpdate()'方法嗎? –
我編輯了我的問題 –
一個粗略的方法是在'Fight'模型中定義'getParentFighterToUpdate',然後就像這樣:'if(class_basename(static :: class)=='PreliminaryFight')'將它們分開向上。這就是說有沒有一個原因,你沒有將其中一個子類傳遞給'chooseAndUpdateParentFight'? –