diff --git a/src/Barryvdh/LaravelIdeHelper/ModelsCommand.php b/src/Barryvdh/LaravelIdeHelper/ModelsCommand.php index 6f2ef6c..aba3e53 100644 --- a/src/Barryvdh/LaravelIdeHelper/ModelsCommand.php +++ b/src/Barryvdh/LaravelIdeHelper/ModelsCommand.php @@ -272,23 +272,21 @@ class ModelsCommand extends Command { $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); - $begin = stripos($code, 'return $this->'); - $parts = explode("'", substr($code, $begin+14),3); //"return $this->" is 14 chars - if (isset($parts[2])) - { - list($relation, $returnModel, $rest) = $parts; - $returnModel = "\\".ltrim($returnModel, "\\"); - $relation = trim($relation, ' ('); - - if($relation === "belongsTo" or $relation === 'hasOne'){ - //Single model is returned - $this->setProperty($method, $returnModel, true, null); - }elseif($relation === "belongsToMany" or $relation === 'hasMany'){ - //Collection or array of models (because Collection is Arrayable) - $this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null); + foreach(array('hasMany', 'belongsToMany', 'hasOne', 'belongsTo') as $relation){ + $search = '$this->'.$relation.'('; + if($pos = stripos($code, $search)){ + $code = substr($code, $pos + strlen($search)); + $arguments = explode(',', substr($code, 0, stripos($code, ')'))); + //Remove quotes, ensure 1 \ in front of the model + $returnModel = "\\".ltrim(trim($arguments[0], " \"'"), "\\"); + if($relation === "belongsToMany" or $relation === 'hasMany'){ + //Collection or array of models (because Collection is Arrayable) + $this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null); + }else{ + //Single model is returned + $this->setProperty($method, $returnModel, true, null); + } } - }else{ - //Not a relation } }