Benjacho / belongs-to-many-field-nova

Belongs To Many field Laravel nova to represent many to many relationship in field.
MIT License
157 stars 80 forks source link

Add suppport for resolveUsing() and displayUsing() functions #26

Closed YarinceP closed 4 years ago

YarinceP commented 4 years ago

Hello, congratulations on this useful package.

I am trying to manipulate the labels that the edit view shows. How can I do it? edit

I already managed to customize the list that shows my elements. but I can't customize the objects in the edit view.

pillbox

pillbox2

YarinceP commented 4 years ago

could you help me? :)

Benjacho commented 4 years ago

Okey share what you were trying

YarinceP commented 4 years ago

Hi @Benjacho , thank you for answering, what I am trying to do I described earlier.

Hello, congratulations on this useful package.

I am trying to manipulate the labels that the edit view shows. How can I do it? edit

I already managed to customize the list that shows my elements. but I can't customize the objects in the edit view.

pillbox

pillbox2

YarinceP commented 4 years ago

@Benjacho If you need any specific clarification regarding my question, do not hesitate to tell me.

YarinceP commented 4 years ago

Any update about this? @Benjacho

techdaddies-kevin commented 4 years ago

I am also trying to achieve something similar. Basically, I have a computed property / Accessor on my related model and that is what I want to use to generate the options list.

techdaddies-kevin commented 4 years ago

@YarinceP Can you share how you were able to customize the output of the dropdown? I am trying to use a computed property on the model to do this (And using the optionsLabel method on the BelongsToManyField class) but it's not working.

YarinceP commented 4 years ago

Of course @techdaddies-kevin . I did it as follows:

add the following to the BelonsToManyField field in your nova resource ->options(\App\User::getMunicipios())

And then he performed a function in the model of his resource. Something like this:

public static function getMunicipios($columns=['*'])
    {
        return DB::table('municipios')
            ->join('estados', 'municipios.estado_id', '=', 'estados.id')
            ->select([
                DB::raw("municipios.id"),
                DB::raw("CONCAT(municipios.nombre,' - ',estados.nombre)  AS nombre")])
            ->get(is_array($columns) ? $columns : func_get_args());
    }
techdaddies-kevin commented 4 years ago

Thanks @YarinceP that helped me tremendously!

@Benjacho I'll try to explain the issue a better to make it more clear what the problem is.

First, I have a custom method on my Category model. This method gets all of my categories, but then changes the name property of each of them to indicate their tiers. image

I then reference this on the BelongsToManyField implementation image

This works great for generating the dropdown, but the problem is that once you've saved the relationship and reload the form, the text input doesn't show the full item shown in the dropdown, it only shows the value from the name column image

I tried to use the optionsLabel method to get the value using a computed property on the model, but it didn't work.

Benjacho commented 4 years ago

Hey could you modify the title method of your reslationship resource to fit with your needs, to display field value names, we are using title method

techdaddies-kevin commented 4 years ago

Not really - If we change the Title attribute it's going to change that title everywhere. We only need this Category > Sub Category > Sub Sub Category organization for this dropdown. On the front-end of the site, we'd want the title to return just the title of that single category. The best solution for us is to allow the dropdown to use a custom attribute to generate the title for the selected items.

Benjacho commented 4 years ago

You can create a function getFullNameAttribute(){ return $this->name . $this->last_name} and append it to model $appends = ['full_name'] it is a mutator

techdaddies-kevin commented 4 years ago

@Benjacho Thank you. I had the custom attribute but I wasn't appending it, which is why it wasn't working.

So now here is my situation:

I have a resource that already has two categories assigned to it. I load the resource edit page, and the dropdown correctly shows the breadcrumbs like I'm wanting (For the two already-set relationships). However, the DROPDOWN does not show the breadcrumb representation like I want.

My opinion is that if the 'optionsLabel()' method is being called, that should also set the display value in the dropdown itself.

image image image

Is there some other way to set the dropdown options that I'm missing? I don't want to update the title property of the resource because I only need to show this full representation in specific scenarios.

Benjacho commented 4 years ago

did you use options method?

mehranhadidi commented 4 years ago

@Benjacho Thank you. Appending the mutator fixed my issue.