Laravel-datatables: columns data issue with name nested relations

Created on 24 Jan 2018  路  1Comment  路  Source: yajra/laravel-datatables

Hello,
on my controller i make this query eloquent query

$tables = MaintenanceDamage::with('ObjectDepartment')
                                   ->whereHas('ObjectDepartment')
                                   ->with('CategoryMaintenanceJob')
                                   ->get();

on datatables i am doing this filtering

   data: function (d) {
                  d.hoteltitle = $('select[name=hoteltitle]').val();
                  d.hoteldepartment = $('select[name=hoteldepartment]').val();
                  d.categorymaintenancejob = $('select[name=categorymaintenancejob]').val();
                  d.damagestatus = $('select[name=damagestatus]').val();
                  d.date_from = $('input[name=date_from]').val();
                  d.date_to = $('input[name=date_to]').val();
              }

but because of the above query my columns is like that

columns: [
                    { data: 'id', name: 'id' },
                    { data: 'object_department.title', name: 'object_department.title' },
                    { data: 'object_department.hotel_department.title', name: 'object_department.hotel_department.title' },
                    { data: 'object_department.hotel_department.hotel.title', name: 'object_department.hotel_department.hotel.title' }, **-> this is the  hoteltitle** 
                    { data: 'category_maintenance_job.title', name: 'category_maintenance_job.title' },
                    { data: 'description', name: 'description' },
                    { data: 'status', name: 'status' },
                    { data: 'created_at', name: 'created_at' },
                    { data: 'priority', name: 'priority'},
                    { data: 'actions', name: 'actions', orderable: false, searchable: false }
                ]

i cannot change this object_department.hotel_department.hotel.title to hoteltitle or the opposite

i want to achieve like when you are doing a query
select(''hotel_categories.title AS hoteltitle)

System details

Operating System
PHP Version 7.2
Laravel Version 5.5
Laravel-Datatables Version "yajra/laravel-datatables-oracle": "~8.0"

question

Most helpful comment

If you want something like select(''hotel_categories.title AS hoteltitle) then you need to use join statements query.

Also, don't use get() on query as it will return a collection.

See also this demo for eager loading ref.

>All comments

If you want something like select(''hotel_categories.title AS hoteltitle) then you need to use join statements query.

Also, don't use get() on query as it will return a collection.

See also this demo for eager loading ref.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hohuuhau picture hohuuhau  路  3Comments

jgatringer picture jgatringer  路  3Comments

sangnguyenplus picture sangnguyenplus  路  3Comments

kamrava picture kamrava  路  3Comments

macnux picture macnux  路  3Comments