Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 检查模型关系_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Laravel 检查模型关系

Laravel 检查模型关系,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我试图检查我的雄辩的模型,以找出它们与其他模型的关系。问题在于,关系仅被定义为单一方法,不存在关系的中心索引: public function posts() { return $this->hasMany('Post'); } 为了检查所有关系,我需要提取方法列表,取出继承自Eloquent的方法,执行每个方法并检查返回类型: $all = get_class_methods($model); $inherited = get_class_methods('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
    }
}

不用说,这是非常危险的。有没有其他更安全的方法来执行此类检查?

您可以将PHPDoc注释添加到关系方法中,然后使用PHP从源代码中提取这些注释