1
我試圖檢查我的Eloquent
模型以找出它們與其他模型的關係。問題是關係簡單地定義爲一個單一的方法和關係的無中心指數存在:檢查模型關係
public function posts()
{
return $this->hasMany('Post');
}
爲了檢查我需要提取的方法列表中的所有關係,採取了從Eloquent
繼承而來的,執行每一個並檢查返回類型:
$all = get_class_methods($model);
$inherited = get_class_methods('Eloquent');
$unique = array_diff($all, $inherited);
foreach($unique AS $method)
{
$relation = $model->$method();
if(is_a($relation, 'Illuminate\Database\Eloquent\Relations\Relation'))
{
//... this is a relation, do something with it
}
}
不用說,這是非常危險的。有沒有辦法以不同的,更安全的方式進行這種檢查?