yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 861 forks source link

Multiple relationships not working #3126

Closed scritrolf closed 7 months ago

scritrolf commented 7 months ago

Summary of problem or feature request

Hi, I have a model that has a number of relationships and I want to have the data of more than one these relationships available. So I use with() to eager load the relationships, but this seems to be working just for one relationship, no method chaining is possible

Code snippet of problem

 public function query(Item $model): QueryBuilder
    {
        return $model->newQuery()->with('category')->with('description_de');
    }
   public function getColumns(): array
    {

        return [
            /*    Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
                */
            // Column::make('id'),
            'Warengruppe' => new \Yajra\DataTables\Html\Column([
                'title' => 'Warengruppe',
                'data' => 'category.description',
            ]),
             'Beschreibung' => new \Yajra\DataTables\Html\Column([
                 'title' => 'Beschreibung',
                 'data' => 'description_de.description',
             ]),
        ];
   }

is what I'm trying to achieve but no matter what order I try them, it always only loads the "category" relationship. If I work with only one "with(...)", the specific relationship works.

scritrolf commented 7 months ago

I dug around a bit and found a solution in another issue here:

  return $model->newQuery()->with('descriptionde')->with('category')->select('items.*');//->with('category');

I'm not really sure why this is needed but it's solved now :)