adminarchitect / core

AdminArchitect - Active Admin for Laravel
http://adminarchitect.com
MIT License
211 stars 66 forks source link

How to display relation's table field? #35

Closed Stiropor closed 5 years ago

Stiropor commented 6 years ago

This is an awesome project! But I have one problem - I can't find in any documentation or here anywhere how to display a field from related table. My events table has a client_id field, that points to clients table. In grid right now the data is displayed at 1. How do I make it dispay's client's name (title field)? I know I need to use update() in columns() method, but how? I see that works just fine in your demo app ... but cannot figure what is the correct way of doing it except manually doing search on Client model... is this the only/right way?

2 more questions:

endihunter commented 6 years ago

Hello.

To display the relations fields, just include them in list of columns $this->push('client.name') with name: 'relationname.fieldname', or in your case 'client.name'. Remember this assumes you have a relation 'client()' in your User model.

There is no difference between cc version and github one. Cc was just for support, and now its easier to "buy a coffee" instead ;)

On Sat, Jul 21, 2018, 14:57 Stiropor notifications@github.com wrote:

This is an awesome project! But I have one problem - I can't find in any documentation or here anywhere how to display a field from related table. My events table has a client_id field, that points to clients table. In grid right now the data is displayed at 1. How do I make it dispay's client's name (title field)? I know I need to use update() in columns() method, but how? I see that works just fine in your demo app ... but cannot figure what is the correct way of doing it except manually doing search on Client model... is this the only/right way?

2 more questions:

  • is that demo site available to download/check anywhere? It'd probably be a great help for users!
  • I see this project is also on codecanyon... what's the difference? I'd gladly support this project, would just like to know what the difference between github's version and one from cc is?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/adminarchitect/core/issues/35, or mute the thread https://github.com/notifications/unsubscribe-auth/ACtA-z3aRWEXi68qp4vaKdqI3lc72fZqks5uIxcygaJpZM4VZml4 .

Stiropor commented 6 years ago

Oh, so basicaly I don't change the client_id but add client and then hide client_id column. Cool. Thanks!

Stiropor commented 6 years ago

2 bugs (I think) I just found:

  1. push doesn't have a position parameter, so I have to use move(). But if I assign ID to client.name in closure, client.name isn't displayed. If I don't assing it, it works, but I have to assign it in order to change it's position.

  2. if I use insert('client.name', ...) and current model also has a name (event.name), it will display event.name instead of client.name. Inserting any other client's field that event doesn't have, works as expected.

I see this app is getting currently updated? The UI is way different than the one on demo. Keep up the good work :)

smallcar88 commented 5 years ago

Hello @endihunter,

I would like to ask how to add a form element based on a user role? Like I have a model User with roles of admin, seller, user I don't want a user record to have an image uploader on the form. How can I conditionally add the form element based on the role value?

thanks!

endihunter commented 5 years ago

@smallcar88 I think something like this should work.

public function form() {
    $form = $this->scaffoldForm();

    if (auth()->user()->isAdmin()) {
        $form->push(FormElement::media('image_uploader'));
    }

    return $form;
}
endihunter commented 5 years ago

@smallcar88 or if you want to disable an element for specific user:

... get form
if (auth()->user()) {
   $form->without('image_uploader');
}

return $form;
smallcar88 commented 5 years ago

@endihunter thank you for the answer. Actually my question is at per-record level, not the current user. For example, user:1 is a seller, user:2 is a normal user. When I edit their profiles as an admin, I want the form of the seller to have image uploader, but not in that of a normal user. Is it achievable via form() function?

endihunter commented 5 years ago

@endihunter thank you for the answer. Actually my question is at per-record level, not the current user. For example, user:1 is a seller, user:2 is a normal user. When I edit their profiles as an admin, I want the form of the seller to have image uploader, but not in that of a normal user. Is it achievable via form() function?

@smallcar88 so, in your case just use the app('scaffold.model') instead of auth()->user(). that will give you the model you're working on instead of logged in user.

smallcar88 commented 5 years ago

Thank you so much @endihunter, your solution works great for me! Much Appreciated!