@vadkertigabor I know this is an old post, but I had the same problem and thanks to this thread I think I found a solution. I will describe my problem in case other people have a similar situation
Problem: rappasoft/laravel-livewire-tables only supports hasOne, belongsTo and morphOne relationships (what it makes sense as you want to show a table). However, I wanted to show a table of multiples tables at once (morphTo).
Solution:
a) Create a new model associated to a temporal table. I called the model TemporalTableModel and the temporal table is temp.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class TemporalTableModel extends Model
{
use HasFactory;
use SoftDeletes;
protected $table='temp';
protected $primaryKey='id';
}
b) In the builder function needed for the rappasoft/laravel-livewire-tables package write the query that you need using. In my case, I needed to use union and join, so I did two subqueries. Then call TemporalTableModel::fromas shown below
public function builder(): Builder
{
$modelA = DB::query()
->select('id', 'table1.name as name1', 'et.name as name2', 'deleted_at')
->from('table1')
->leftJoin('extraTable as et', 'param1', '=', 'et.param2');
$modelB = DB::query()
->select('id', 'table2.name as name1', 'et.name as name2', 'deleted_at')
->from('table2')
->leftJoin('extraTable as et', 'param1', '=', 'et.param1');
$subQuery = $modelA->union($modelB);
$query = TemporalTableModel::from( DB::raw("({$subQuery->toSql()}) as temp") ); // This is equivalent to SELECT * FROM ( your_sub_query ) AS temp
return $query;
}
c) Create configure and columns functions according to your query
public function configure(): void
{
$this->setPrimaryKey('id');
}
public function columns(): array
{
return [
Column::make('ID', 'id')
->sortable()
->searchable(),
Column::make('Name 1', 'name1')
->sortable()
->searchable(),
Column::make('Name 2', 'name2')
->sortable()
->searchable()
];
}
With this, the package will apply all its options (search, order, pagination) to the temp table that you created with your sub query. I hope it helps you.