topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.12k stars 130 forks source link

Introduce Integration with barryvdh/laravel-ide-helper #77

Closed mpyw closed 8 months ago

mpyw commented 4 years ago

I mentioned here:

It should be introduced also on README or Wiki.

<?php

namespace App\Console\Commands\IdeHelper;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand as BaseModelsCommand;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Relations\Relation;

class ModelsCommand extends BaseModelsCommand
{
    /**
     * @param  string   $relation
     * @param  Relation $relationObj
     * @return bool
     */
    protected function isRelationNullable(string $relation, Relation $relationObj): bool
    {
        $reflectionObj = new \ReflectionObject($relationObj);

        if (in_array($relation, ['hasOne', 'hasOneThrough', 'morphOne'], true) || !$reflectionObj->hasProperty('foreignKey')) {
            return parent::isRelationNullable($relation, $relationObj);
        }

        $fkProp = $reflectionObj->getProperty('foreignKey');
        $fkProp->setAccessible(true);

        return (bool)Arr::first(
            (array)$fkProp->getValue($relationObj),
            fn (string $value) => isset($this->nullableColumns[$value])
        );
    }
}
yurii-github commented 3 years ago

hello

it looks like you know this subject very closely. maybe you can make a PR for it?

afaik, this is something that cannot be done on compoships but only on laravel-ide-helper, right?

calebdw commented 8 months ago

Hello!

This issue has been fixed by the linked PR and can be closed