mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 18:13:11 +00:00
Allow multi-line relations
Fixes #56 Searches for $this->hasMany( etc. Also allows both " and ' quotes.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user