CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

[Bug] Add HasOneThrough on relationship pro column

Open dimer47 opened this issue 1 year ago • 3 comments
trafficstars

Bug report

What I did

Add HasOneThrough in the relationship column pro field.

What I expected to happen

Show the value of my HasOneThrough relationship in the relationship column.

TODO

Show the value of my HasOneThrough relationship in the relationship column. File : vendor/backpack/pro/resources/views/columns/relationship.blade.php

Code :

{{-- relationships (switchboard; supports both single and multiple: 1-1, 1-n, n-n) --}}
@php
    $allowsMultiple = $crud->guessIfFieldHasMultipleFromRelationType($column['relation_type']);
    switch($column['relation_type']) {
        case 'HasOne':
        case 'MorphOne':
            $column['type'] =  isset($column['subfields']) ? 'repeatable' : 'text';
        break;
        case 'HasMany':
        case 'MorphMany':
        case 'BelongsToMany':
        case 'MorphToMany':
            $column['type'] = isset($column['subfields']) ? 'repeatable' : ($allowsMultiple ? 'select_multiple' : 'select');
        break;
        case 'BelongsTo':
        case 'HasOneThrough':
        case 'MorphTo':
            $column['type'] = 'select';
        break;
        default:
            $column['type'] = 'text';
    }
@endphp

@includeFirst(\Backpack\CRUD\ViewNamespaces::getViewPathsFor('columns', $column['type']))

Instead of

{{-- relationships (switchboard; supports both single and multiple: 1-1, 1-n, n-n) --}}
@php
    $allowsMultiple = $crud->guessIfFieldHasMultipleFromRelationType($column['relation_type']);
    switch($column['relation_type']) {
        case 'HasOne':
        case 'MorphOne':
            $column['type'] =  isset($column['subfields']) ? 'repeatable' : 'text';
        break;
        case 'HasMany':
        case 'MorphMany':
        case 'BelongsToMany':
        case 'MorphToMany':
            $column['type'] = isset($column['subfields']) ? 'repeatable' : ($allowsMultiple ? 'select_multiple' : 'select');
        break;
        case 'BelongsTo':
        case 'MorphTo':
            $column['type'] = 'select';
        break;
        default:
            $column['type'] = 'text';
    }
@endphp

@includeFirst(\Backpack\CRUD\ViewNamespaces::getViewPathsFor('columns', $column['type']))

dimer47 avatar Jun 02 '24 13:06 dimer47

Hey @dimer47 hope you are doing well man 🙏

I haven't tested it, but if it's that simple we can surely give it a go.

Want to submit a PR for it or should I pick it from here ?

Cheers

pxpm avatar Jun 03 '24 09:06 pxpm

Hello @pxpm,

I'm fine thank you, I hope you are too? I wanted to make a PR but it is a file present in the backpack PRO repo which is not a public repo or a repo to which I have access. This is why I created an issue instead of directly making a PR.

Am I wrong, do I have access to the backpack/pro repo? If so, could you tell me how?

Cheers

dimer47 avatar Jun 03 '24 10:06 dimer47

Hello @pxpm,

I'm fine thank you, I hope you are too? I wanted to make a PR but it is a file present in the backpack PRO repo which is not a public repo or a repo to which I have access. This is why I created an issue instead of directly making a PR.

Am I wrong, do I have access to the backpack/pro repo? If so, could you tell me how?

Cheers

Hey @dimer47 sorry you are right.

I will add it myself and get back here with news. 🙏

Thanks

pxpm avatar Jun 03 '24 11:06 pxpm

Hello @pxpm,

Have you had the time to review/test my proposal to improve the relationship field for a quick update of the pro package?

Best regards, Dimer47

dimer47 avatar Jul 15 '24 10:07 dimer47

Sorry @dimer47 totally slipped.

This should be an easy one, I will reserv time today to do it 👍

Thanks for the heads up. 🙏

pxpm avatar Jul 15 '24 10:07 pxpm

Hey @dimer47

It's tagged in backpack/pro 2.2.6, a composer update should get you the updated version.

// in model
 public function petAvatar()
    {
        return $this->hasOneThrough(\App\Models\PetShop\Avatar::class, \App\Models\PetShop\Pet::class, 'id', 'avatarable_id', 'pet_id', 'id');
    }

// in controller
CRUD::column('petAvatar');

Thanks again for the suggestion and providing the solution 🙏

Let me know if something does not work as you expected 👍

Cheers

pxpm avatar Jul 15 '24 18:07 pxpm

Hey @pxpm,

Thank you! I updated the dependencies of my project, and now I can use this feature. Awesome, thanks a lot!

Cheers

dimer47 avatar Jul 18 '24 10:07 dimer47