Git Product home page Git Product logo

Comments (11)

ryangjchandler avatar ryangjchandler commented on May 10, 2024 2

@ryangjchandler yeah that's what I used for this simple use case and it makes sense as I can reuse this accessor. My other case was to build a popover for cell that shows a full user object on hover but I can most likely just do another query from the custom view with the user id there.

Hm, I'll take a look at this later on if I get a chance - see if there's any sensible way for us to do this.

from filament.

ryangjchandler avatar ryangjchandler commented on May 10, 2024 1

@zltn Here's what you want to do:

Text::make('myHasOneRelation')
    ->getValueUsing(function ($record) {
        return $record->myHasOneRelation;
    })
    ->formatUsing(function ($myHasOneRelation) {
        return $myHasOneRelation->name;
    })

If you have any questions, please feel free to re-open this issue and we can look at ways of improving the developer experience.

from filament.

danharrin avatar danharrin commented on May 10, 2024

Hey! Are you using the dot syntax to retrieve the display column from the related table?

e.g. Columns\Text::make('myHasOneRelation.name')

from filament.

jamessessford avatar jamessessford commented on May 10, 2024

I had a look to see if I could reproduce this and it seems to be the behaviour you would expect if you weren't trying to retreive a field, but the whole relationship.

Columns\Text::make('myHasOneRelation') doesn't return anything, presumably as it's returning a model which can't be converted wheras Columns\Text::make('myHasManyRelation') is returning a collection which will be converted to a JSON string

from filament.

ryangjchandler avatar ryangjchandler commented on May 10, 2024

@jamessessford Yeah, I can't see any reason why someone would need to retrieve the whole relationship, so I wouldn't say this is a bug more of an unexpected scenario.

@zltn If you want to show some information about the relation, you should use the dot-notation syntax as mentioned by @danharrin or you could also use an accessor on the model itself.

from filament.

jamessessford avatar jamessessford commented on May 10, 2024

@ryangjchandler definitely

from filament.

zltn avatar zltn commented on May 10, 2024

The dot syntax works and I can do a workaround for this model. My original approach was to pass on the entire object and use the formatUsing() method on the entire object. @ryangjchandler this would be the big use case for which you'd want the entire object. Same for passing on the object to a custom view with view() which I'm planning for another model.

from filament.

ryangjchandler avatar ryangjchandler commented on May 10, 2024

@zltn You can use an accessor to get your custom "formatted" data.

from filament.

zltn avatar zltn commented on May 10, 2024

@ryangjchandler yeah that's what I used for this simple use case and it makes sense as I can reuse this accessor. My other case was to build a popover for cell that shows a full user object on hover but I can most likely just do another query from the custom view with the user id there.

from filament.

pathros avatar pathros commented on May 10, 2024

Regarding the HasOne error:

Could you please explain with more detail? I mean, in which file or namespace in Filament Admin should I try?

In my case, in a ModelResource file, inside the

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                 //a hasOne relationship defined in my model
                 Forms\Components\BelongsToSelect::make('myHasOneRelation')->relationship('myHasOneRelation','myHasOneRelation.id')->required()->helperText('ID '),//hasOne relationship
            ])
    }

method, how do I implement a "hasOne" relationship in a create / edit form? Because I get the following error:

TypeError
Filament\Forms\Components\BelongsToSelect::getRelationship(): Return value must be of type ?Illuminate\Database\Eloquent\Relations\BelongsTo, Illuminate\Database\Eloquent\Relations\HasOne returned

from filament.

pathros avatar pathros commented on May 10, 2024

Moreover, I have a "BelongsToThrough" relationship, defined in my model like so:

 public function myrelationship2(){
        return $this->belongsToThrough(User::class,Event::class,'id','',[Event::class=>'event_id',User::class=>'responsable_id']);
    }

How would I implement it inside my ModelResources "$form->schema()" and "$table->columns()"?

from filament.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.