kdion4891 / laravel-livewire-tables

A dynamic, responsive Laravel Livewire table component with searching, sorting, checkboxes, and pagination.
302 stars 41 forks source link

Sorting relationships on table #45

Open SigalZ opened 2 years ago

SigalZ commented 2 years ago

Hello,

Thank you for this package.

I do have some issues though.

I have these tables: coffees, roast_types, green_beans.

In the Coffee model I have these relationships:

public function roast_type()
    {
        return $this->belongsTo(RoastType::class);
    }

     public function green_bean()
    {
        return $this->belongsTo(GreenBean::class);
    }

In the CoffeeTable component I have this query:

return Coffee::with('roast_type')
        ->with('green_bean')
            ->where('coffees.active', $this->active)
            ->where('blend', 0);

and these columns:

return [
            Column::make('ID')->searchable()->sortable(),
            Column::make('Name')->searchable()->sortable(),
            Column::make('Green Bean', 'green_bean.name')->searchable()->sortable(),
            Column::make('Roast Type', 'roast_type.name')->searchable()->sortable(),
            Column::make()->view('admin.coffee.table_actions'),
        ];

I have 2 problems:

  1. When I click on sorting the Roast Type column the values in the Name column change to the Roast type name instead of the Coffee name.

  2. When I click on sorting the Green Bean column, I get this error:

Missing required parameter for [Route: coffees.edit] [URI: coffees/{coffee}/edit] [Missing parameter: coffee]. (View: C:\wamp64\www\highland8.local\resources\views\admin\coffee\table_actions.blade.php)

What am I doing wrong?

BTW, I added some functionality like in the jquery dropdown tables, to show:

  1. A dropdown for the user to select how many records to display on the page
  2. A paragraph that shows how many records are displayed from the total results

Can you please help with my issues?

gbrits commented 2 years ago

Did you ever get sorting worting for a relationship, I'm facing the same thing and my table is just turning up empty whenever I sort by a relationship?

SigalZ commented 2 years ago

Hi @gbrits, Yes I did, I don't remember exactly how.

I think I created my own datatable classes and view where I do the db queries myself.

I can send you the files if you want.