Consider the following:
Define the fallback function to your Model:
public function __call($name, $arguments)
{
if (strpos($name, 'modulesData') !== false) {
$nameArray = explode(' ', $name);
$moduleName = ucfirst($nameArray[1]);
$moduleClass = 'App\Modules\\' . $moduleName . '\\' . $moduleName;
return $this->modulesData($moduleClass);
} else {
return parent::__call($name, $arguments);
}
}
Use the with
function with this way:
$tal = \App\Model::with('modulesData ModuleName')->get();
(being 'ModuleName' the name of the module you want to use as parameter for the relationship).
This way you can eagerload with the string "modulesData_moduleName
". When with
is invoked it won't find that function and will fallback to __call
, which will extract the "moduleName" and call the relationship "modulesData" with it as parameter.