staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relations not shown in artisan command model:show

cpuch opened this issue · comments

Hello,

I have a Client model with a HasManyDeep relation as follow

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use App\Models\ListItem;
use App\Models\RoyaltiesArtist;
use App\Models\RoyaltiesContrat;

class Client extends Model
{
    use HasFactory;
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    [...]

    public function artistContracts(): HasManyDeep
    {
        return $this->hasManyDeep(
            RoyaltiesContrat::class,
            [RoyaltiesArtist::class, ListItem::class], // Intermediate models, beginning at the far parent (Client).
            [
                'id_client',          // Foreign key on the royalties_artist table.
                'id_item',            // Foreign key on the list_item table.
                'id_list_info_artist' // Foreign key on the royalties_contrat table.
            ],
            [
                'id_client', // Local key on the client table.
                'id',        // Local key on the royalties_artist table.
                'id_list',   // Local key on the list_item table.
            ]
        );
    }

    [...]
}

When I use the artisan command the relation is not displayed

 php artisan model:show Client

  App\Models\Client ................................................................................................................................
  Database .............................................................................................................................. soundworks
  Table ..................................................................................................................................... client

  Attributes ........................................................................................................................... type / cast
  id_client increments, unique .............................................................................................. integer unsigned / int

  [...]

  Relations ........................................................................................................................................
  password HasOne .............................................................................................................. App\Models\Password
  artist HasOne ......................................................................................................... App\Models\RoyaltiesArtist
  bands HasMany ........................................................................................................... App\Models\RoyaltiesBand
  bandContracts HasManyThrough ......................................................................................... App\Models\RoyaltiesContrat

  Observers ........................................................................................................................................

It would be nice if the artisan command could return the relationship.

Hi @cpuch,
The model:show command doesn't support custom relations like HasManyDeep and doesn't have a way to add them.

This needs to be changed in Laravel. I'll look into submitting a PR when I find the time.

I have to close this issue. I've looked into it but I don't see a viable solution for the Laravel command to support third-party relations.

If you want/need to have this feature in your app, you can create your own model:show command by extending ShowModelCommand and overriding $relationMethods.