singlequote / Laravel-datatables

This repo contains a Datatable that can render a filterable and sortable table. It aims to be very lightweight and easy to use. It has support for retrieving data asynchronously, pagination and recursive searching in relations.
https://singlequote.github.io/Laravel-datatables/
MIT License
18 stars 6 forks source link

search and ordering not working on nested relationship #55

Open happymacarts opened 2 years ago

happymacarts commented 2 years ago

I have a model that has a hasOne relation "Info" that also has a hasOne relationship "subject"

//table model
public $columns = [
  ...  ,
  [
      'data'  => 'info.subject.name',
       'name'  => 'info.subject.name',
       'class' => 'subjectArea',
       'searchable' => false,
       "columnSearch" => true,
  ],
];

public function query($query)
{
    return $query
        ->whereHas('info', function($query){
                // $query->with('subject_area'); // have tried quite a few things here still lost
        })
        ->with('user', 'status', 'info', 'info.subject') //tried multiple things here as well just can't find right combination
        ->withTrashed();

//initial model
public function info()
{
    return $this->hasOne(\App\Models\EnrollmentAssistanceInfo::class);
}

//EnrollmentAssistanceInfo model
public function subject()
{
    return $this->hasOne(\App\Models\CourseSubjectArea::class, 'id', 'subject_area');
}

when I attempt to do a search the query is broken.

Column not found: 1054 Unknown column 'subject' in 'where clause'

what do I need to do to my query function to make it search and sort the subject field? and when i try to sort that column the ajax goes out but no sorting happens and i do not receive an error.

wimurk commented 2 years ago

Hi @happymacarts, the max level of nesting is 1 at the moment. This is because the queries are created without elequent. This fueture is still open.