InfyOmLabs / laravel-generator

API and Admin Panel CRUD Generator for Laravel.
https://www.infyom.com/open-source
MIT License
3.79k stars 812 forks source link

Get data from relationship into datable using htmlBuilder #548

Closed cesarureno closed 6 years ago

cesarureno commented 6 years ago

How can get custom data from relationship? I need display country name

screen shot 2018-04-14 at 02 07 09
dhcmega commented 6 years ago

Hi Use the "with" method on the query to eager load the relation and add the column:

'country_id' => ['name' => 'countries.name', 'data' => 'countries.name', 'title' => 'Country'],

PS: what theme are you using?

cesarureno commented 6 years ago

Thank you @dhcmega

dhcmega commented 5 years ago

@cesarureno Hi, can you please tell which style you've used for the table? I need something like your screenshot. Thanks!

cesarureno commented 5 years ago

I used a theme https://pixinvent.com/stack-responsive-bootstrap-4-admin-template/html/ltr/horizontal-menu-template-nav/dt-styling.html

mitulgolakiya commented 5 years ago

Blog Post for this: https://blog.infyom.com/how-to-display-relationship-data-into-yajra-datatables-with-infyom-laravel-generator

dhcmega commented 5 years ago

@mitulgolakiya It might be a good idea to add NAME tag along side with DATA to that post. Even though it's not the scope of the post.

Not doing so, when using search, it will trigger a MySQL error if relation is composed of multiple words.

No including 'name' => 'surveyQuestion.question', will generate this error:

Column not found: 1054 Unknown column 'survey_question.question'

(note the missing S in table's name)

mitulgolakiya commented 5 years ago

@dhcmega

(note the missing S in table's name)

I didn't get this line.

dhcmega commented 5 years ago

Hi! Sorry, maybe I'm wrong. I remember I have tested it and if you don't include the 'name' element in the array, only 'data' with the eloquent relation, when using search feature the table name gets rendered as the relation in snake case in singular, without the final S that the table will probably have. In my test, the table name is survey_questions and the relation is 'surveyQuestion', but it gets converted to 'survey_question.question' and MySQL give and error. This problem gets solved by also adding 'name' element. Does it make sense? Thanks again

mitulgolakiya commented 5 years ago

yes.

shailesh-ladumor commented 4 years ago

here is tutorial