for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace LWS\Import\Traits;
use ErrorException;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Database\Eloquent\Relations\Relation;
trait RelationshipsTrait
{
public function relationships()
$model = new static;
$relationships = [];
foreach ((new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class != get_class($model) ||
! empty($method->getParameters()) ||
$method->getName() == __FUNCTION__) {
continue;
}
try {
$return = $method->invoke($model);
if ($return instanceof Relation) {
$relationships[$method->getName()] = [
'type' => (new ReflectionClass($return))->getShortName(),
'model' => (new ReflectionClass($return->getRelated()))->getName(),
];
} catch (ErrorException $e) {
return $relationships;