mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-21 19:43:10 +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(');
|
$begin = strpos($code, 'function(');
|
||||||
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
|
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
|
||||||
|
|
||||||
$begin = stripos($code, 'return $this->');
|
foreach(array('hasMany', 'belongsToMany', 'hasOne', 'belongsTo') as $relation){
|
||||||
$parts = explode("'", substr($code, $begin+14),3); //"return $this->" is 14 chars
|
$search = '$this->'.$relation.'(';
|
||||||
if (isset($parts[2]))
|
if($pos = stripos($code, $search)){
|
||||||
{
|
$code = substr($code, $pos + strlen($search));
|
||||||
list($relation, $returnModel, $rest) = $parts;
|
$arguments = explode(',', substr($code, 0, stripos($code, ')')));
|
||||||
$returnModel = "\\".ltrim($returnModel, "\\");
|
//Remove quotes, ensure 1 \ in front of the model
|
||||||
$relation = trim($relation, ' (');
|
$returnModel = "\\".ltrim(trim($arguments[0], " \"'"), "\\");
|
||||||
|
if($relation === "belongsToMany" or $relation === 'hasMany'){
|
||||||
if($relation === "belongsTo" or $relation === 'hasOne'){
|
//Collection or array of models (because Collection is Arrayable)
|
||||||
//Single model is returned
|
$this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null);
|
||||||
$this->setProperty($method, $returnModel, true, null);
|
}else{
|
||||||
}elseif($relation === "belongsToMany" or $relation === 'hasMany'){
|
//Single model is returned
|
||||||
//Collection or array of models (because Collection is Arrayable)
|
$this->setProperty($method, $returnModel, true, null);
|
||||||
$this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null);
|
}
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
//Not a relation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user